The situation is the following:
<select>
tags with options loaded from DB.The problem is the following: if I use an XML file and there are some errors in the form fields, the struts framework doesn't pass through the class method I laid out, but it will directly return the input
result. So what's the point? That in this way I can't load the options for the various <select>
tags I mentioned above.
So I thought to do something like this:
<result name="input" type="chain">
<param name="actionName">Class_method</param>
</result>
but with this trick I lose all the error messages, i.e. hasFieldErrors()
returns always false.
How can I solve that?
Many questions, all good though.
Conversion and validation errors forces the Workflow
interceptor to trigger the INPUT
result, and the workflow will execute the INPUT
result instead of reaching the action method (execute()
or whatever).
If you need to populate some static data, like selectboxes sources, that must be available also in case of INPUT
result, you should put that loading in a prepare()
method, and make your action implement the Preparable
interface. This method is run by an Interceptor before the INPUT result is returned, as described in the official docs.
Avoid using the chain
result. It is officially discouraged since many years.
If you want to prevent double submits (by pressing F5 after a page has been submitted and the result rendered), you can use the PRG pattern with the redirectAction
result. This way, however, you'd encounter the same problem of the chain
result: the messages (and the parameters) will be lost.
To preserve the error messages, action errors and field errors across the redirections, you can use a predefined interceptor called Message Store
Interceptor, that you must include in your stack because the defaultStack
doesn't include it. I've described how it works in this answer.
If you decide to use the Message Store along with PRG there are more considerations, too long to be written here, but that could be explained in the future, about preventing infinite recursion due to Field Error -> INPUT -> PRG -> Retrieve Field Error -> INPUT -> etc...
that will be blocked by the browser near the 10th recursion... but that's another story.