1

The situation is the following:

  1. I have a JSP page with a form.
  2. This form contains various <select> tags with options loaded from DB.
  3. I want to use validation with an XML file.

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?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
zer0uno
  • 7,521
  • 13
  • 57
  • 86

2 Answers2

1

Many questions, all good though.

  1. 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).

  2. 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.

  3. Avoid using the chain result. It is officially discouraged since many years.

  4. 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.

  5. 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.

  6. 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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks a lot. Just one thing, I tried and the Preparable interface *must* be implemented to let prepare interceptor call the prepare method. – zer0uno Jun 15 '16 at 13:14
  • 1
    You're right, `Preparable` is not natively implemented by `ActionSupport`, as I mis-remembered; I have `Preparable` implemented on my BaseAction that is extended by all the other actions... write once and forget :) Thank you – Andrea Ligios Jun 15 '16 at 14:08
0

One option:

public class Foo extends ActionSupport {
    public string myAction() { return SUCCESS; }
    public void validateMyAction() { // executed after XML validation
        // other complex validation here if needed
        if (hasErrors()) {
            // repopulate form data from DB here
        }
    }
}

hasErrors() method comes from the ValidationAware interface which ActionSupport implements.

Another option is to do a redirect on input result and use the message store interceptor to keep action messages

binoternary
  • 1,865
  • 1
  • 13
  • 25