Different action and actionform combinations in struts

Source: Internet
Author: User
Tags empty html form reset
See a good article on tss.com about the pros and cons of various action and actionform combinations used in struts. I first digest, tidy up, for everyone to refer to. Original title: Struts action mappings:divide Et Impera, Author: Michael Juravlev. The url:http://www.theserverside.com/articles/article.tss?l=strutsactionmapping on the TSS

Explanation: Reading this article needs a certain struts foundation.
Note: The action of small and medium writes does not necessarily represent the concrete struts action class, sometimes also refers to as a whole action mapping.


[1] Full action

<action path= "/afullaction"
Type= "Somepackage.someactionclass" >
Name= "Someform"
input= "Somejsp.jsp"
<forward name= "Successful" path= "somejsp.jsp"/>
<forward name= "Failed" path= "someotherjsp.jsp"/>
</action>

First, the actionservlet of struts receives a request, It then navigates to the corresponding mapping (map) based on the configuration of the Struts-config.xml, and then creates a new form instance if the form is scoped to request, or if the form is not found in the defined scope, and then the form instance is called. () method, and then place the parameters in the form into the form, if the Validate property is not false, call the Validate () method, or if validate () returns a Non-empty actionerrors, will be transferred to the URI specified by the input property. If an empty actionerrors is returned, execute the Execute () method of the action to determine the target URI based on the returned Actionforward.

The effect is that execute () executes only after the Validate () succeeds, and the input property specifies a URI.


[2] There is only a form action

<action path= "/aformonlyaction"
Type= "Org.apache.struts.actions.ForwardAction"
Name= "Someform"
input= "Somejsp.jsp"
Parameter= "Someotherjsp.jsp"
/>

First, struts will search for someform in the defined scope, reuse if found, and create a new instance if it is not found; When you get the form instance, call its reset () method, and then put the parameters from the form into the form, if the Validate property is not false , the Validate () method is invoked, and if validate () returns a Non-empty actionerrors, it will be forwarded to the URI specified by the input property, and if an empty actionerrors is returned, go to the target URI specified by the parameter property.

The effect is that no action class can store our business logic, so all the logic that needs to be written can only be written to the reset () or validate () method of the form. The role of validate () is to authenticate and access the business layer. Because the action map here does not include forward (and meaningless), it cannot be redirected, only the default forward. This form-only action can be used to process data acquisition and forward to another JSP to display.


[3] action-only action

<action path= "/anactiononlyaction"
Type= "Somepackage.someactionclass" >
input= "Somejsp.jsp"
<forward name= "Successful" path= "somejsp.jsp"/>
<forward name= "Failed" path= "someotherjsp.jsp"/>
</action>

First, Actionservlet receives the request, obtains an action class instance, invokes the Execute () method, and then actionforward the Forward,forward to the specified URI or action in the configuration based on the return.

The effect of this is that no form instance is passed in to the Execute () method, so execute () must get the parameters from the request itself. The action can be forward or redirected. This action does not handle requests submitted through HTML form, and can only handle linked-style requests.


[4] only the JSP action

<action path= "/ajsponlyaction"
Type= "Org.apache.struts.actions.ForwardAction"
Parameter= "Someotherjsp.jsp"
/>

First, Actionservlet invokes the Forwardaction execute () method upon receipt of the request, and execute () forward to that URI based on the configured parameter property value.

The effect is that no form is instantiated, the more realistic scenario may be that the form is defined at the higher level of the request, or that the action is used to act as a system parameter after the application is compiled, only to change the configuration file without having to recompile the system.


[5] Two action corresponds to a form

<action path= "/anaction"
Type= "Somepackage.someactionclass" >
Name= "Someform"
input= "Somejsp.jsp"
<forward name= "Successful" path= "/anotheraction.do"/>
</action>
<action path= "/anotheraction"
Type= "Somepackage.someotheractionclass" >
Name= "Someform"
input= "Someotherjsp.jsp"
<forward name= "Successful" path= "someresultjsp.jsp"/>
</action>

In terms of each individual action, there is no real difference between the processing and the complete action. This combination pattern can be used to pass the Command object (form). Note that the reset () and validate () methods of form are also invoked in the latter action, so we have to make sure that the information in the form is not overridden.

The approach is broadly divided into two types: A in the request, put an indicator indicating that the previous action intends to pass the form backwards, so that the value in that form can be retained at the latter action, which is only used when using forward. b when using redirect instead of forward, you can put the indicator at session or higher level and clear the indicator in the last loop of the command chain.


[6] Two action corresponds to two form

<action path= "/anaction"
Type= "Somepackage.someactionclass" >
Name= "Someform"
input= "Somejsp.jsp"
<forward name= "Successful" path= "/anotheraction.do" redirect= "true"/>
</action>
<action path= "/anotheraction"
Type= "Somepackage.someotheractionclass" > "
Name= "Someotherform"
input= "Someotherjsp.jsp"
<forward name= "Successful" path= "someresultjsp.jsp"/>
</action>

This combination is not much different in the process, but we now provide a form for two action, and the code looks clearer. So we can handle the input and output of the Web application separately. It is worth noting that the latter action will also attempt to write those parameters to the form, but we can do this: A to use another set of property names in the latter form and B to provide only the getter without the setter.

The approximate treatment is this:
The previous action receives input, validates, then writes the data to the business or persistence layer, redirects to the latter action, and the latter action manually pulls the data from the business layer/persistence layer, writes the form (by other means) to the foreground JSP.

The advantage of doing this is that you do not have to keep the values in the input form, so you can use redirect instead of forward. This reduces the coupling between the two action and avoids unnecessary duplicate submissions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.