Struts: jsp cannot display message controlled by action

P

PC Leung

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>
 
R

Ryan Stewart

PC Leung said:
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.
 
P

PC Leung

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.
 
P

PC Leung

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
 
R

Ryan Stewart

PC Leung said:
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?
 
P

PC Leung

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?
 
P

PC Leung

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
 
P

PC Leung

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?
 
R

Ryan Stewart

PC Leung said:
to let other JSP use the same link


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top