• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ActionMessages in STRUTS 1.1 Please help !!!

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
I am developing a webbased struts application.
Due to changes in struts version 1.2 an derros we get (ClassCastException in ErrorsTag.java), I decided to change the Actions from ActionErrors to ActionMessages.


When I try with some validation and database erros, the AxctrionMessages object will be filled correctly.

But when I want to display them, nothing is displayed.

This is my source code:

AbstractAction:
protected void setActionMessages(ActionMessages messagesToAdd, HttpServletRequest request) {
if((messagesToAdd != null) && (messagesToAdd.size() > 0)){
saveMessages(request, actionMessagesToAdd);
}
else return;
}

Action:
ActionMessages actionMessages = null;
try {

actionMessages = form.validateCustom(mapping, request);
if (actionMessages!=null && !actionMessages.isEmpty()) {
setActionMessages(actionMessages, request); //Here I use the abstract method
return findActionForward(mapping, form.getActionForward());
}

...

actionMessages = prepareData(data, request);
if ((actionMessages != null) && (actionMessages.size() > 0)){
saveMessages(request, actionMessages);
return findActionForward(mapping, form.getActionForward());
}
else {
actionMessages = checkData(data);
}
if ((actionMessages != null) && (actionMessages.size() > 0)){
saveMessages(request, actionMessages);
return findActionForward(mapping, form.getActionForward());
}
else {
actionMessages = validateSpecial(data, request);
}

and so on...

In the jsp, I have:
<logic:messagesPresent>
<h3>Errors:</h3>
<html:messages id="msg">
<bean:write name="msg"/>
<br>
</html:messages>
</logic:messagesPresent>

For example, when the method "prepareData();" gets an erro, it will be hold in the actionMessages object.
Thats ok.
This means, the saveMessages() method will be called.
After that the "findActionForward()" method will be called an the jsp will be shown.
Now I would expect to see the messages in the header of the jsp.
But I still see the same JSP like before, no error message etc.

What I am doing wrong ???

Please help !

best regards
Michael
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

<logic:messagesPresent>
<h3>Errors:</h3>
<html:messages id="msg">
<bean:write name="msg"/>
<br>
</html:messages>
</logic:messagesPresent>

Have you defined msg in your ApplicationResource.properties, I mean the property file that you are using.

In Struts1.2 you can still use ActionErrors its only ActionError that is depercated and so we can use ActionMessage in place of that. I just suggest this, perhaps then you can still use your old code with small changes for ActionError with ActionMessage.
 
Michael Zohner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My application property file is not located under WEB-INF.

WEB-INF is located under ../../WEB-INF.

SOURCE (where my application.properties file is located) is located under

../SOURCE

Should I take the old code (I already changed every ActionErrors to ActionMessages) and ONLY change ...new ActionError(...) to new ActionMessage(...) ???

This means that there will be a ActionMEssage object in a ActionErrors collection. Is that right ?

regards
Michael
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, the tag will look for Globals.ERROR_KEY but when you save with saveMessages(), the messages are saved under the Globals.MESSAGE_KEY attribute.

You need:

<html:messages ... message="true">

Also, why do you keep checking (messages != null && messages.size() > 0) in your code? You really don't need to do that. You don't need the setActionMessages() method either. saveMessages() will handle the cases when messages is null or empty.
 
Michael Zohner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means, I should make it like this ?

AbstractAction: no setError() method

Action:
actionErrors = form.validateCustom(mapping, request);
if (actionErrors!=null && !actionErrors.isEmpty()) {saveErrors(request, actionErrors);
return findActionForward(mapping, form.getActionForward());
}

form.validateCustom(mapping, request); does this:
...
actionErrors.add(Globals.ERROR_KEY, new ActionMessage("Internal error"));
...

and so on...

Is that right ?

How the code in the JSP should look like ?

Anything to add to the config.xml ?

best regards
Michael
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that ActionError and all the methods related to ActionError specifically, e.g. saveErrors(request, ActionErrors) are deprecated. Use ActionMessage and saveMessages(). Just add message="true" to your html:messages tag in the JSP. You don't need anything in the config.xml.
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Should I take the old code (I already changed every ActionErrors to ActionMessages) and ONLY change ...new ActionError(...) to new ActionMessage(...) ???

This means that there will be a ActionMEssage object in a ActionErrors collection. Is that right ?



Yes thats what I mean, just replace all new ActionError() with new ActionMessage() in your old code without changing ActionErrors with ActionMessages.

and also the properties file should be in class folder under WEB-INF folder and not in source folder (where I suppose you have java files and not class files.) I mean the properties file should be under classpath.
 
Michael Zohner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much !!!

I will use the saveErrors() method for this app version with Struts 1.1, for the next version in January I will use saveMessages().

But, can I use the following ?

ActionErrors = new ActionErrors();

...new ActionMessage(..., ...)

...
saveMessages();
...

Could I use the saveMessage() method when still putting an ActionMessage into ActionErrors instead of saveErrors() ?

best regards
Michael
 
reply
    Bookmark Topic Watch Topic
  • New Topic