Analysis on the functions of Action attributes in JSP

Source: Internet
Author: User

1. Complete 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 Struts ActionServlet receives a request, and then according to the configuration of the struts-config.XML to locate the corresponding mapping ); next, if the form range is request or it is difficult to find this form in the defined range, create a new form instance. After obtaining the form instance, call its reset () method, then, put the parameters in the form into the form. If the validate attribute is not false, The validate () method is called. If validate () returns non-empty ActionErrors, the URI specified by the input attribute will be transferred. If an empty ActionErrors is returned, execute () method of the Action to determine the target URI Based on the returned ActionForward.

The result is that execute () is executed only after validate () is successful, and the input attribute specifies a URI.

2. Only Form actions

﹤action path="/aFormOnlyAction"
type="org.apache.struts.actions.ForwardAction"
name="someForm"
input="someJSP.jsp"
parameter="someOtherJSP.jsp"
/>

First, Struts searches for someForm in the defined scope. If it is found, it is reused. If it is difficult to find, a new instance is created. After obtaining the form instance, Struts calls its reset () method, then, put the parameters in the form into the form. If the validate attribute is not false, The validate () method is called. If validate () returns non-empty ActionErrors, the URI specified by the input property will be transferred. If an empty ActionErrors is returned, the URI specified by the parameter property will be transferred.

The result is that no action class can store our business logic, so any logic to be written can only be written to form's reset () or validate () method. Validate () is used to verify and access the business layer. Because the action ing here does not include forward and does not make sense), you cannot redirect, but you can only use the default forward. This form-only action can be used to process data acquisition and forward to another JSP for display.

3. 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, after receiving the request, the ActionServlet obtains the action class instance and calls the execute () method. Then, based on the returned ActionForward, it finds forward in the configuration and forward to the specified URI or action.

The result is that no form instance is passed in the execute () method, so execute () must obtain parameters from the request. Actions can be forwarded or redirected. Such actions cannot process requests submitted through html form, but can only process link requests.

4. JSP-only actions

﹤action path="/aJSPOnlyAction"
type="org.apache.struts.actions.ForwardAction"
parameter="someOtherJSP.jsp"
/﹥

First, the ActionServlet calls the execute () method of ForwardAction after receiving the request. execute () will forward to the URI according to the configured parameter attribute value.

The result is that no form is instantiated. The more realistic situation may be that form is defined in the scope of higher request level; or this action is used as a system parameter after the application is compiled. You only need to modify this configuration document without re-compiling the system.

5. Two actions correspond to one 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﹥

For each separate action, there is no substantive distinction between the processing and the complete action. This combination mode can be used to pass the command object form ). Note that the reset () and validate () Methods of form are also called in the next action. Therefore, we must ensure that the information in form is not overwritten.

There are roughly two processing methods:

A) Put an indicator in the request to indicate that the previous action intends to pass the form to the next action, so that the value of the form can be retained in the next action, this method can only be used when forward is used. B) When redirect is used instead of forward, the indicator can be placed at the session level or higher, and this indicator is cleared at the last part of the command chain.

﹤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 method is not much different from the previous one in the process, but we now provide form for the two actions separately, so the code looks clearer. Therefore, we can process the input and output of Web applications separately. It is worth noting that the next action will try to write those parameters to the form, but we can do this:

A) use another set of attribute names in the next form;

B) Only getter is provided instead of setter.

The general solution is as follows:

The previous action receives input, verification, and then writes data to the business layer or persistent layer, redirects it to the next action, and the latter action manually extracts data from the business layer/persistent layer, write the form to the front-end JSP.

The advantage of this is that you do not have to retain the value in the input form, so you can use redirect instead of forward. This reduces the coupling between two actions and avoids unnecessary repeated submissions.

  1. JSP best practices use JSTL to update JSP pages
  2. Comparison of JSP and ASP. NET stored procedures
  3. Analysis of the best solution examples for JSP and IIS

Related Article

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.