Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change 
notification.

The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/StrutsCatalogRedirectToInputPage

The comment on the change is:
Orphaned; better options available.

------------------------------------------------------------------------------
+ deleted
- ## page was renamed from StrutsCatalogRedirectOnError
- ## page was renamed from StrutsCatalogUsingValidate
- First a short recap of how Struts processes request.
-    * Form bean is initialized.
-    * reset() is called.
-    * Fields are populated.
-    * validate() is called.
-       * if validate() returns non-null object, Struts skips action class 
altogether, and selects path corresponding to "input" property.
-       * If validate() returns null, then execute() is called on action class.
  
- So, if error occurred in the input, then action class is not bothered at all. 
But what happens with the user experience? By default "input" property is used 
to '''forward''' to input/error page, it does not have "redirect" flag. That 
means, that if a user submitted a form and made a mistake, the application 
shows an error page immediately in response to POST request. Thus, when the 
user tries to reload the input/error page, he gets "Do you want to resend 
POSTDATA?" message generated by browser, which is not nice by itself. And if 
the user clicks "Yes", he gets into a double submit situation.
- 
- The proper thing to do would be to separate input phase from output phase, 
and to prevent these kinds of implicit resubmits. By the way, Ruby On Rails 
specifically declare this I/O separation as their official approach to request 
processing. Spring can do the same for more than a year. JSF allows to do this 
too.
- 
- I/O separation is done using redirection:
- 
-    * User submits input data using POST
-    * Data is validated, the proper view is chosen
-    * A user is redirected to the view location
-    * A browser loads the view using GET request
- 
- A new !ActionForward subclass,  
[http://struts.apache.org/api/org/apache/struts/action/ActionRedirect.html 
ActionRedirect], allows adding request parameters at runtime to the redirected 
request. It is available starting with Struts 1.2.7.
- 
- With redirection a user can refresh result page anytime, or browse back and 
forward without causing resubmit. He is happy, data is intact, and no tokens 
needed. Oops, we forgot that "input" property, which sends a user to the 
input/error page, does not allow redirection by default.
- 
- This is not a problem, Struts has an "inputForward" attribute of <controller> 
element in struts-config.xml, which should be set to true. This parameter means 
that the "input" attributes of all the <action> elements are not URLs anymore, 
but are names of local or global !ActionForward element. This setting is global 
for the whole application.
- 
- ''In the struts-config.xml''
- {{{
-     <controller inputForward="true" />
- 
-     <action path="..." input="myInput" validate="true">
-         <forward name="myInput" path="..." redirect="true" />
-     </action>
- }}}
- 
- "inputForward" property allows to redirect to input/error page, but now there 
is another problem: the error messages are gone. This happens because errors 
returned by validate() are stored in request object, which is destroyed on 
redirect.
- 
- To preserve errors you need to do the following:
-    * Before returning from validate() save error messages in the session.
-    * After redirection completes, pull errors from the session and stick them 
into request object.
- It is important to remove messages from the session, so they would not be 
redisplayed on some other page.
- 
- You can automate this process by using Action.saveMessages(!HttpSession 
session, !ActionMessages messages) method. It saves messages in the session, 
and then !RequestProcessor automatically removes them from the session after 
they were accessed. 
- 
- If you need to use saveErrors() instead of saveMessages(), then you can use 
the folliwing workaround (by Matt Raible):
- 
- {{{
- session.setAttribute(Globals.ERROR_KEY, messages);
- }}}
- 
- And in the JSP:
- 
- {{{
- <%-- Error Messages --%>
- <logic:messagesPresent>
-     <div class="error">
-         <html:messages id="error">
-             <bean:write name="error" filter="false"/><br/>
-         </html:messages>
-         <!-- Error messages aren't automatically removed -->
-         <c:remove scope="session" var="org.apache.struts.action.ERROR"/>
-     </div>
- </logic:messagesPresent>
- }}}
- 
- or you can wait for next Struts version which will have saveErrors() method 
saving to the session.
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to