Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Struts: jsp cannot display message controlled by action

Reply
Thread Tools

Struts: jsp cannot display message controlled by action

 
 
PC Leung
Guest
Posts: n/a
 
      07-19-2004
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>
 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      07-19-2004
"PC Leung" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


 
Reply With Quote
 
 
 
 
PC Leung
Guest
Posts: n/a
 
      07-20-2004
thank you I will try.

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

> redirect="true"/>
> ...
> A redirect doesn't preserve the request.

 
Reply With Quote
 
PC Leung
Guest
Posts: n/a
 
      07-20-2004
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.

 
Reply With Quote
 
PC Leung
Guest
Posts: n/a
 
      07-20-2004
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
 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      07-21-2004
"PC Leung" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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?


 
Reply With Quote
 
PC Leung
Guest
Posts: n/a
 
      07-21-2004
> 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?
 
Reply With Quote
 
PC Leung
Guest
Posts: n/a
 
      07-21-2004
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
 
Reply With Quote
 
PC Leung
Guest
Posts: n/a
 
      07-21-2004
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?
 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      07-21-2004
"PC Leung" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> > 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:
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
display confirm message box for action Grey ASP .Net 7 09-07-2004 04:30 PM
Cannot find message resources under key org.apache.struts.action.MESSAGE Joos Nielsen Java 2 06-03-2004 11:30 PM
Cannot find message resources under key org.apache.struts.action.MESSAGE Hari Om Java 3 10-01-2003 08:17 PM
Struts Static Action Form vs Dyanamic Action Form Joe Bloggs Java 1 08-03-2003 02:30 AM



Advertisments