Struts2 -- custom interceptor

Source: Internet
Author: User

 

The struts2 interceptor is one of the core functions of the struts framework. First, the automatic data filling function implemented by the struts framework is implemented by the interceptor. Here we will summarize the experiences of the blocker during this period.

It is obvious that the interceptor must be pre-processed or post-processed. The parameter filling implemented by the framework itself is in the preprocessing phase. In type conversion, do not perform preprocessing or post-processing. In the preprocessing phase, convert the string data from the view to the corresponding java data type, after the business processing is complete, convert the result type into a string to the result page for display.

To sum up the usage of the custom Interceptor:

1. Create an interceptor class to implement the Logic Functions of the interceptor. Interceptor interface must be implemented

2. Configure struts. xml to add the interceptor.

3. Associate the interceptor with the corresponding action.

4. Test the interceptor.

Here we use the most common authentication to implement a custom interceptor.

 

 

 

Public class LimitInterceptor implements Interceptor {

/**

*

*/

Private static final long serialVersionUID = 1L;

 

 

Public void destroy (){

}

 

 

Public void init (){

}

 

 

Public String intercept (ActionInvocation invocation) throws Exception {

If (user logged on ){

Return invocation. invoke ();

} Else {

Return "Log ";

}

}

}

Public class LimitInterceptor implements Interceptor {

/**

*

*/

Private static final long serialVersionUID = 1L;

 

 

Public void destroy (){

}

 

 

Public void init (){

}

 

 

Public String intercept (ActionInvocation invocation) throws Exception {

If (user logged on ){

Return invocation. invoke ();

} Else {

Return "Log ";

}

}

}

 

The logic function of the Interceptor class is implemented first. The Interceptor interface has three methods: destory, init, and intercept. Init and destory are mainly used to initialize and clean up resources. The main business logic is in intercept. The main logic function here is to check whether a user is logged on. If the user is logged on, the invocation is performed. invoke () indicates that the execution continues. If you have not logged on to the console, go to a public view-Log. This is in struts. A public view declared in xml, that is, the logon interface.

 

<Interceptors>

<Interceptor name = "permission" class = "interceptor. LimitInterceptor"/>

<Interceptor-stack name = "permissionStack">

<Interceptor-ref name = "permission"/>

<Interceptor-ref name = "defaultStack"/>

</Interceptor-stack>

</Interceptors>

<Interceptors>

<Interceptor name = "permission" class = "interceptor. LimitInterceptor"/>

<Interceptor-stack name = "permissionStack">

<Interceptor-ref name = "permission"/>

<Interceptor-ref name = "defaultStack"/>

</Interceptor-stack>

</Interceptors> Configure the interceptor in struts. xml, which is similar to the lifecycle action. It is worth noting that if we customize the interceptor and associate it with an action, the system's interceptor will be blocked by default, and most of the core work of struts2 cannot be completed, there are two solutions: one is to add the system default interceptor when adding an interceptor for each action; second, we wrap the system interceptor together with our own interceptor into an interceptor stack and then use it. Here we use the second method, because it facilitates modification and maintenance.

 

<Action name = "DeleteStudent" class = "stumanage. action. DeleteStudent" method = "execute">

<Interceptor-ref name = "permissionStack"/>

</Action>

<Action name = "DeleteStudent" class = "stumanage. action. DeleteStudent" method = "execute">

<Interceptor-ref name = "permissionStack"/>

</Action> here, an action is DeleteStudent. We add an interceptor for this action, that is, if you want to delete a student, you must have the permission, that is, you must log on.

Use <interceptor-ref name = "permissionStack"/> to associate our encapsulated interceptor stack with this action. In this way, the custom interceptor can work normally. If you do not log on, you are not authorized to delete students.

 

Of course, we can also use

 

<Default-interceptor-ref name = "permissinStack"/> to make our custom interceptor stack a default interceptor, that is, to add this interceptor stack for each action.

Author Ling Feng

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.