Struts 2 interceptor about solving login problems

The working principle of the interceptor is shown in the figure. Each action request is wrapped in a series of interceptors, and the request is sent again through redirectaction.

The interceptor can perform similar operations on the action execution line or recycle after the action execution line.

We can let each action either transfer the operation to the following interceptor, or the action can directly exit the operation and return to the customer's established screen.

Next, how to define an Interceptor:

Customize an interceptor as follows:

1. Implement the interceptor interface or inherit the abstractinterceptor abstract class.

2. Create a struts XML file to define interceptors.

3. The interceptor defined above is referenced in the action to be used. For convenience, the interceptor can also be defined as the default interceptor (< default interceptor ref name = "mystack" / >),

In this way, all actions are intercepted by this interceptor without special declaration < param name = "excludemethods" > loginview, login < / param >.

① The interceptor interface declares three methods:

② Let it inherit methodfilterinterceptor:

③ Useraction inherits actionsupport to implement modelDriven < user > and sessionaware:

Struts. In the XML file:

Among them, < param name = "excludemethods" > loginview, login < / param > configured filtering methods, which means that the interceptor has no effect on the methods. In my case, loginview is the method to jump to the login page.

Login is the method to verify the user name and password, in which the authenticated user name will be put into the session.

Summary:

1. In struts 2, all interceptors will inherit the interceptor interface.

2. If we do not add interceptors, struts 2 will add default interceptors for us. Of course, if we specify interceptors, our own interceptors will replace the default interceptors,

Then we can't enjoy some of the functions provided by the default interceptor. So, I usually add the default interceptor.

For example, in the above configuration items, add < interceptor ref name = "defaultstack" > < / interceptor ref > to action

The above article on solving the login problem of the struts 2 interceptor is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>