validationjspstruts2tilesstruts2-tiles-plugin

Validation with one action and multiple JSP pages in Struts 2


I have a form with a section where I submit a comment separate from updating the entire form.

To keep my JSP manageable I use the following Struts 2 action:

    <action name="maininfo" class="MainInfo">
        <result name="success" type="tiles">maininfo</result>
        <result name="addFundingComment" type="tiles">addFundingComment</result>
        <result name="input" type="tiles">maininfo</result>
    </action>    

The maininfo tile displays the main form JSP page. The addFundingComment tile displays the form to submit the comment.

My issue is if validation fails the "input" result has to go to either the maininfo tile or the addFundingComment tile but I need it to go to the tile corresponding to the form the validation failed for.

I can get around this by putting the addFundingComment JSP code in the maininfo JSP code and using a <s:if> to display the form I want but I think having two separate JSP files makes each one easier to manage and debug.

I want to use one Action to make it easier to keep all of the maininfo field changes when the user submits a comment.

Is there a way to have one Action with <results> which go to different JSP pages and have validation failures return to the corresponding JSP page?


Solution

  • You can use dynamic result configuration to return the corresponding view. The dynamic property is evaluated via OGNL, so you have to create a getter method to return the location for the input result.

    <result name="input" type="tiles">${inputName}</result>
    

    In the action class

    public String getInputName() {
      String inputName = "maininfo";
      if (actionName.equals("addFundingComment")
         inputName = "addFundingComment";
      return inputName;
    }