• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionErrors (Struts)

 
dawn cal
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your action, are you calling saveErrors?
For example:
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I cover that in my article:
http://www.javaranch.com/newsletter/Mar2002/newslettermar2002.jsp#struts
 
dawn cal
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I do in the perform method of my Action class.
 
Jason Menard
Sheriff
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic