aspose file tools*
The moose likes Struts and the fly likes ActionMessages in STRUTS 1.1 Please help !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ActionMessages in STRUTS 1.1 Please help !!!" Watch "ActionMessages in STRUTS 1.1 Please help !!!" New topic
Author

ActionMessages in STRUTS 1.1 Please help !!!

Michael Zohner
Greenhorn

Joined: Nov 24, 2004
Posts: 9
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
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
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

Joined: Nov 24, 2004
Posts: 9
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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Michael Zohner
Greenhorn

Joined: Nov 24, 2004
Posts: 9
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
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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

Joined: Oct 18, 2004
Posts: 346

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

Joined: Nov 24, 2004
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionMessages in STRUTS 1.1 Please help !!!