1

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
ponder275
  • 903
  • 2
  • 12
  • 33
  • 1
    Sounds like you've conflated two different actions. The most trivial solution is to submit to a different method in the same action. Another trivial solution is to include a hidden field identifying the form, or calculate it based on what form data is present. – Dave Newton Jun 22 '17 at 15:05
  • I do submit to a different method in the action. When they click on "Add A Comment" I submit the method which returns "addFundingComment" which displays the "addFundingComment.jsp" page. When they click on "Submit Comment" I submit the method which stores the comment in the database and returns "success" which displays the "maininfo.jsp" page. I agree I can hide the comment form in the "maininfo.jsp" but I was trying to have two smaller jsp files instead of one huge one. – ponder275 Jun 22 '17 at 15:14
  • 2
    I'm saying have two actions configured with the method specified in the action config. Then you can have separate "input" results and let the framework do the work instead of having to do it manually. – Dave Newton Jun 22 '17 at 15:16

1 Answers1

1

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;
} 
Roman C
  • 49,761
  • 33
  • 66
  • 176