Struts: jsp cannot display message controlled by action

Discussion in 'Java' started by PC Leung, Jul 19, 2004.

  1. PC Leung

    PC Leung Guest

    The jsp file cannot display message sent from action.
    The following is my coding.
    Pls help.


    ApplicationResources.properties:
    statusLine.recordAdded=Record Added

    addUserProfile.jsp:
    <logic:messagesPresent>
    <hr>
    <bean:message key="messages.header"/>
    <ul>
    <html:messages id="actionMessage" property="statusLine" message="true">
    <li>Messages:<bean:write name="actionMessage"/>
    </li>
    </html:messages>
    </ul>
    </logic:messagesPresent>

    AddUserProfileAction.java:
    ActionMessages actionMessages = new ActionMessages();
    ActionMessage actionMessage = new
    ActionMessage("statusLine.recordAdded");
    actionMessages.add("statusLine", actionMessage);
    saveMessages (request, actionMessages);
    return mapping.findForward("addUserProfile");

    struts-config.xml:
    <global-forwards
    type="org.apache.struts.action.ActionForward">
    <forward name="addUserProfile" path="/addUserProfile.jsp"
    redirect="true"/>
    </global-forwards>

    <action path="/addUserProfile"
    type="com.erp.quotation.AddUserProfileAction"
    name="addUserProfileForm"
    scope="request"
    validate="true"
    input="/addUserProfile.jsp">
    </action>
     
    PC Leung, Jul 19, 2004
    #1
    1. Advertising

  2. PC Leung

    Ryan Stewart Guest

    "PC Leung" <> wrote in message
    news:...
    > The jsp file cannot display message sent from action.
    > The following is my coding.
    > Pls help.
    >

    ....
    > return mapping.findForward("addUserProfile");
    >

    ....
    > <forward name="addUserProfile" path="/addUserProfile.jsp"

    redirect="true"/>
    ....
    A redirect doesn't preserve the request.
     
    Ryan Stewart, Jul 19, 2004
    #2
    1. Advertising

  3. PC Leung

    PC Leung Guest

    thank you I will try.

    > ...
    > > <forward name="addUserProfile" path="/addUserProfile.jsp"

    > redirect="true"/>
    > ...
    > A redirect doesn't preserve the request.
     
    PC Leung, Jul 20, 2004
    #3
  4. PC Leung

    PC Leung Guest

    After removing redirect="true",
    1) Same situation: message dose not show up.
    2) create another problem:
    the data inputted in the form get re-display
    on the form again after transaction completed.


    > ...
    > > return mapping.findForward("addUserProfile");
    > >

    > ...
    > > <forward name="addUserProfile" path="/addUserProfile.jsp"

    > redirect="true"/>
    > ...
    > A redirect doesn't preserve the request.
     
    PC Leung, Jul 20, 2004
    #4
  5. PC Leung

    PC Leung Guest

    I preserve the 'redirect="true"'

    and add one line to action mapping
    ==> <forward name="addUserProfile" path="/AddUserProfile.jsp">
    like the following:

    <action path="/addUserProfile"
    type="com.erp.quotation.AddUserProfileAction"
    name="addUserProfileForm"
    scope="request"
    validate="true"
    input="/addUserProfile.jsp">
    <forward name="addUserProfile" path="/AddUserProfile.jsp">
    </action>

    Then the messages can be displayed by JSP.

    However all the data on the form get re-display
    after transaction is written to MySQL database.
    How can I clear form data after completion?

    If I remove the line
    <forward name="addUserProfile" path="/AddUserProfile.jsp">
    form data will not be displayed again after transaction
    completes, but messages disappear.

    Adding that line can make messages appear but form data redisplays again.

    How to cope with this?

    Thanks
     
    PC Leung, Jul 20, 2004
    #5
  6. PC Leung

    Ryan Stewart Guest

    "PC Leung" <> wrote in message
    news:...
    > I preserve the 'redirect="true"'
    >
    > and add one line to action mapping
    > ==> <forward name="addUserProfile" path="/AddUserProfile.jsp">
    > like the following:
    >
    > <action path="/addUserProfile"
    > type="com.erp.quotation.AddUserProfileAction"
    > name="addUserProfileForm"
    > scope="request"
    > validate="true"
    > input="/addUserProfile.jsp">
    > <forward name="addUserProfile" path="/AddUserProfile.jsp">
    > </action>
    >
    > Then the messages can be displayed by JSP.
    >

    All you did was mask the global forward. If it's used by other actions,
    you'll have the same problem. If it isn't, why is it a global forward?

    > However all the data on the form get re-display
    > after transaction is written to MySQL database.
    > How can I clear form data after completion?
    >
    > If I remove the line
    > <forward name="addUserProfile" path="/AddUserProfile.jsp">
    > form data will not be displayed again after transaction
    > completes, but messages disappear.
    >
    > Adding that line can make messages appear but form data redisplays again.
    >
    > How to cope with this?
    >
    > Thanks


    What do you mean the form data redisplays? When? If you've written a class
    extending ActionForm, have you overridden the reset method?
     
    Ryan Stewart, Jul 21, 2004
    #6
  7. PC Leung

    PC Leung Guest

    > All you did was mask the global forward. If it's used by other actions,
    > you'll have the same problem. If it isn't, why is it a global forward?


    to let other JSP use the same link

    > What do you mean the form data redisplays? When? If you've written a class
    > extending ActionForm, have you overridden the reset method?


    after user clicks the submit button, its Action class will insert record
    to database. Then the form should be cleared and a successful message
    displays below the form.
    However the form data is not cleared after clicking the submit button.
    Successful message is able to be displayed below form.


    I write the reset method like this.

    public void reset(ActionMapping mapping, HttpServletRequest request) {
    action = null;
    firstName = null;
    middleName = null;
    lastName = null;
    dob = null;
    joinDate = null;
    }

    Am I doing it in the proper way of using Struts?
     
    PC Leung, Jul 21, 2004
    #7
  8. PC Leung

    PC Leung Guest

    I have to make it clear that I am using ValidatorForm.
    In case of input error, form data re-display on screen
    is good.
    But if input data is OK, I want the form to be
    cleared after clicking submit button with successful
    message displaying below form.

    Based on the coding, what should be done?
    Thanks
     
    PC Leung, Jul 21, 2004
    #8
  9. PC Leung

    PC Leung Guest

    After inserting a line in the following before "return mapping.findForward"
    in Action class, form data can be cleared.

    addUserProfileForm.reset();

    Now messages can be displayed and form data can be cleared
    after press submit button.

    Is it a standard way of using Struts?
     
    PC Leung, Jul 21, 2004
    #9
  10. PC Leung

    Ryan Stewart Guest

    "PC Leung" <> wrote in message
    news:...
    > > All you did was mask the global forward. If it's used by other actions,
    > > you'll have the same problem. If it isn't, why is it a global forward?

    >
    > to let other JSP use the same link
    >
    > > What do you mean the form data redisplays? When? If you've written a

    class
    > > extending ActionForm, have you overridden the reset method?

    >
    > after user clicks the submit button, its Action class will insert record
    > to database. Then the form should be cleared and a successful message
    > displays below the form.
    > However the form data is not cleared after clicking the submit button.
    > Successful message is able to be displayed below form.
    >
    >
    > I write the reset method like this.
    >
    > public void reset(ActionMapping mapping, HttpServletRequest request) {
    > action = null;
    > firstName = null;
    > middleName = null;
    > lastName = null;
    > dob = null;
    > joinDate = null;
    > }
    >
    > Am I doing it in the proper way of using Struts?


    Oh, you're having a form submission forward back to the form? You'll have to
    either reset the form manually or set the fields to have an initial value of
    "". Here's what happens when the HTML form is submitted:
    1) Examine html:form's action attribute and determine which form bean is to
    be used
    2) Instantiate or find existing instance of ActionForm
    3) Call reset method of the ActionForm
    4) Populate the ActionForm from the data submitted in the HTML form
    5) Execute appropriate method in your Action
    6) Forward control to HTML form
    7) Since the page you've forwarded to has an html:form, perform steps one
    and two
    ** Here's the kicker:
    8) Use form bean instance to populate html:form fields

    As you can see, since the form bean exists with the data you submitted, when
    you forward back to the same form, it's going to, by design, auto-populate
    with the data which is contained in the form bean.
     
    Ryan Stewart, Jul 21, 2004
    #10
  11. PC Leung

    PC Leung Guest

    Thank you, Ryan.
     
    PC Leung, Jul 22, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Bloggs
    Replies:
    1
    Views:
    991
    Sudsy
    Aug 3, 2003
  2. Hari Om
    Replies:
    3
    Views:
    12,292
    Wendy S
    Oct 1, 2003
  3. Joos Nielsen
    Replies:
    2
    Views:
    1,051
    Ryan Stewart
    Jun 4, 2004
  4. rjweytens
    Replies:
    6
    Views:
    16,357
    rjweytens
    Jun 25, 2004
  5. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,955
    runescience
    Feb 7, 2006
Loading...

Share This Page