Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp me better understand Struts2, validation, and stateful actions
    text
    copied!<p>As I understand it, Struts2 action class instances can (unlike Struts1) be stateful, because each GET or POST to an action creates a new instance of the backing action class.</p> <p>I also see that there's a standard(?) idiom (Pattern?) to provide input forms: the same .jsp is used as the View component of two different actions, like this:</p> <pre><code>&lt;action name="showForm" class="defaultActionThatDoesNothingExceptReturnSuccess"&gt; &lt;result name="success"&gt;inputForm.jsp&lt;/result&gt; &lt;/action&gt; &lt;action name="validateAndProcessForm" class="realAction"&gt; &lt;result name="input"&gt;inputForm.jsp&lt;/result&gt; &lt;result name="success"&gt;formProcessed.jsp&lt;/result&gt; &lt;/action&gt; </code></pre> <p>The first action just displays the form, without validating the input or processing it. The form in the .jsp posts to the <em>second</em> action:</p> <pre><code>&lt;s:form action="validateAndProcessForm" method="post"&gt; </code></pre> <p>and that second action validates the posted fields/parameters, returning "input" if the form's inputs are incomplete or invalid, or actually calling the action class's <code>execute</code> if the inputs are complete and valid, thus processing the form and returning the (<em>e.g.</em>) <code>formProcessed.jsp</code> that displays something like "thanks for your input!".</p> <p>So we have this sort of "picket fence" idiom:</p> <pre><code>defaultAction- -&gt; realAction- | | | | -&gt; input.jsp- &lt;--- -&gt; success.jsp </code></pre> <p>This is done so that the first time <code>input.jsp</code> is displayed, validations aren't called (and so validation errors are not shown), but that after the submit button on that jsp is clicked, the "real" action will validate the input, possibly passing back errors calling out invalid input which the <code>input.jsp</code> will display. </p> <p>Which brings us back to stateful, non-singleton actions; because the action is stateful and thus not shared across GETs or POSTs, and each instance is instantiated just for that GET or POST, the action has no way to know if a particular session has "GETted" the same page multiple times. So GETting <code>showForm.action</code> will <em>never</em> validate, and GETing <code>validateAndProcessForm</code> will <em>always</em> validate (and show errors if the parameters are invalid), <em>even if</em> that GET is the first time a particular session has "GETted" that URL.</p> <p>Which is why we need the "fence post": the first action just to display the form, the second to capture the input.</p> <p>Is my understanding correct? Is there a less verbose way to do this, to not validate input on the initial GET, but to validate on the POST, without having to have two actions for every form?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload