aspose file tools*
The moose likes Struts and the fly likes ActionErrors (Struts) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ActionErrors (Struts)" Watch "ActionErrors (Struts)" New topic
Author

ActionErrors (Struts)

dawn cal
Greenhorn

Joined: Aug 23, 2002
Posts: 4
Using Jakarta Struts: I am trying to display errors that occur in my Action classes in a jsp page. I though I could just create an ActionErrors collection and forward the action to a jsp and display the errors using the tag <html:errors>. However, it appears that this only works for ActionForms (via the validate method). Is there a way to display errors that occur in the Action class or do I have to create my own custom tag? Thank for the help!
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
In your action, are you calling saveErrors?
For example:
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I think I cover that in my article:
http://www.javaranch.com/newsletter/Mar2002/newslettermar2002.jsp#struts


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
dawn cal
Greenhorn

Joined: Aug 23, 2002
Posts: 4
Thanks Thomas, that is a great article. However, it explains how to use ActionErrors with the validate method of the ActionForm. I don't have a form associated with this particular action.
I do use saveErrors() to save the errors to the request. I am just having trouble displaying them. The tag <html:errors/> displays the error.header and error.footer but not the error messages. I tried checking for errors in the jsp page:
ActionErrors ae = (ActionErrors) request.getAttribute(Action.ERROR_KEY);
if(ae != null){
out.println("There are errors!!");

for(int x=0; x<ae.size(); x++){
out.println(ae.get(ae.GLOBAL_ERROR));
}
}
But this is what is printed to the page:
java.util.AbstractList$Itr@e7d503 java.util.AbstractList$Itr@51dcd6
Thanks for your help!
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
As long as you are adding ActionError objects to the ActionErrors collection correctly and using saveErrors(), then <html:errors/> should work. Can you show us what code you use in your action to instantiate the relevant objects (ActioneErrors and ActionError objects), add them to the collection, and save them?
Please use the UBB CODE tag for readability when you post code, thanks.
[ August 26, 2002: Message edited by: Jason Menard ]
dawn cal
Greenhorn

Joined: Aug 23, 2002
Posts: 4
This is what I do in the perform method of my Action class.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I guess we can assume that your errors object is not empty and the conditional clause is being executed. You said it displays the header and footer, so that tells me that the taglib is being imported and that ApplicationResources.properties is correctly definied in your web.mxl.
Are you certain that "error.task.notassigned" is definied in your ApplicationResources.properties? Other than that possibility, everything looks correct and I can't see any problems.
dawn cal
Greenhorn

Joined: Aug 23, 2002
Posts: 4
You are right Jason... I didn't have my error message defined in my properties file. I am such a dummy. Thank you so much for all your help!!
Ahhhhhhhhhhh!!!
Prasanna Soundarapandiyan
Greenhorn

Joined: Sep 04, 2009
Posts: 9
In Struts higher versions (above 1.2.0 i guess ), the usage of ActionError is depricated, hence the following code will help to resolve it if you want ,

Use your code similar to the following in your action class to add the action errors,

ActionErrors errors = null;
errors = (ActionErrors) request.getAttribute(Globals.ERROR_KEY);
if (errors == null) {
errors = new ActionErrors();
}
errors.add("Global Error message", new ActionMessage("error.provided.in propertiesfile"));
request.setAttribute(Globals.ERROR_KEY, errors);

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionErrors (Struts)