Struts2 in Paramsprepareparamsstack Interceptor

Source: Internet
Author: User

In Struts2, the Interceptor is the essence of the frame, while the two-time parameter Interceptor Paramsprepareparamsstack has a good processing mechanism for solving the problem of data echo and object modification property loss.

The following sections are available in the default interceptor configuration in Struts-default.xml (self-locating source code, usually: struts-2.5.2\src\core\src\main\resources):

1<Interceptor-stackname= "Paramsprepareparamsstack">2<Interceptor-refname= "Exception"/>3<Interceptor-refname= "Alias"/>4<Interceptor-refname= "i18n"/>5<Interceptor-refname= "checkbox"/>6<Interceptor-refname= "datetime"/>7<Interceptor-refname= "MultiSelect"/>8<Interceptor-refname= "params"/>           <!--encapsulates the parameters submitted in the JSP into the Action object---(1) -9<Interceptor-refname= "ServletConfig"/>Ten<Interceptor-refname= "Prepare"/>          <!--predecessor Method----------------------------(2) - One<Interceptor-refname= "Chain"/> A<Interceptor-refname= "Modeldriven"/>      <!--Place the non-empty model on top of the stack----------------(3) - -<Interceptor-refname= "FileUpload"/> -<Interceptor-refname= "Staticparams"/> the<Interceptor-refname= "Actionmappingparams"/> -<Interceptor-refname= "params"/>           <!--encapsulates the JSP submission parameter in the Action object-------(4) - -<Interceptor-refname= "Conversionerror"/>                              <!--Conversion Exception - -<Interceptor-refname= "Validation">                                    <!--Verifying Exceptions - +<paramname= "Excludemethods">Input,back,cancel,browse</param> -</Interceptor-ref> +<Interceptor-refname= "Workflow">                                      <!--handle the above 2 exceptions - A<paramname= "Excludemethods">Input,back,cancel,browse</param>      <!--Exclude Unwanted - at</Interceptor-ref> -</Interceptor-stack>

(1), about <interceptor-ref name= "params"/>

Encapsulates the parameters of a page submission into an object in the action, only the attributes declared in the action can be accepted and encapsulated, that is, only the given attribute is accepted, (when multiple parameters are filtered, they are only encapsulated with a given condition)

The action must also provide a Get,set method.

// Example: Private Long ID;  Public Long getId () {4    return ID; 5 }publicvoid  setId (Long id) {7 this    . id = ID; 8}

Into this action, the top element of the stack is:employeeaction

( the action is accessed by default, the action is on top of the stack )

(2), <interceptor-ref name="Prepare"/>

Emlpoyeeaction.java

1/**2 * Use the interceptor that is configured in action 3 * automatically performs 4 * completion parameter echo before the input Method 5*/6 Public voidPrepareinput ()throwsException {7if(id! =NULL) { 8 employee = employeeservice.get (ID);// Echo Echo9     }10 }11 12//Automatic execution before the Save method13//solve the problem of missing parameters14 Public voidPreparesave ()throwsException {15if(id! =NULL) {Employee = Employeeservice.get (ID);//guaranteed attributes do not lose critical code17}Else {Employee =NewEmployee ();19     }20 }21 22 23//last execute public void prepare () method24//Automatic execution before all strtus methods: Order of interceptors25@Override26 Public voidPrepare ()throwsException {27//related features, or leave blank28}

(3), <interceptor-ref name="Modeldriven"/> Drive model, accompanied by pressure stack operation

Employeeaction.java

1 @Override 2  Public Employee Getmodel () {3     return employee;  // This returns the object that Preoare has been validated to receive. 4 }

Into this action, the top element of the stack is:Employee

( because in the case of a two-time parameter interceptor, whether it is from the database or the new object, there is a relative to the previous content of the same class of objects, so the stack operation will be performed, the object is placed on the top of the stack )

Focus:

<interceptor-ref name="params"/>---------------------------------------------------------------- ----------------(4)

Object (1), and the object obtained by the previous preparexxx () method (2),

That is, the object at the top of the stack (2) compares the attributes, and if the attributes in 1 are missing, the missing attributes are made up and then subsequently manipulated.

At last:

Executes the method of the page call in action, returns the result view, and makes a page jump.

Also, after configuring play two parameter interceptors, check the contents of the Struts.xml configuration file again

Struts2 in Paramsprepareparamsstack Interceptor

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.