This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
In your action, are you calling saveErrors? For example:
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
posted
0
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
posted
0
This is what I do in the perform method of my Action class.
Jason Menard
Sheriff
Joined: Nov 09, 2000
Posts: 6450
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.