The Struts2 interceptor solves the logon problem. The struts2 interceptor logs on.

Source: Internet
Author: User

The Struts2 interceptor solves the logon problem. The struts2 interceptor logs on.
1. Understand the Struts2 Interceptor [Interceptor]

The principle of the interceptor is that each action request is encapsulated in a series of interceptors, And the request is sent again through redirectAction.

The interceptor can perform similar operations in a straight line of an Action, or perform a recycle operation after the Action is executed directly.

Each Action can be transferred to the interceptor below, and the Action can exit directly to return the customer's established picture.

Next, how do we define an interceptor:

 A custom interceptor is as follows:

1. Implement the Interceptor interface or inherit the AbstractInterceptor abstract class.

2. Create a Struts. xml file to define the interceptor.

3. reference the interceptor defined above in the Action to be used, for convenience, you can also define the interceptor as the default interceptor (<default-interceptor-ref name = "myStack"/> ),

In this way, all actions are intercepted by the interceptor without special declarations <param name = "excludeMethods"> loginView and login </param>.

① The Interceptor interface declares three methods:

1 public class LoginInterceptor implements Interceptor {2 3 private Map <String, Object> session = null; 4 public void destroy () {} 5 public void init (){}
6 public String intercept (ActionInvocation actionInvocation) throws Exception {8 Object myAction = actionInvocation. getAction (); 9 if (myAction instanceof UserAction) {10 System. out. println ("the Action you access is UserAction. Do not verify the Session; otherwise, an endless loop"); 11 // allow 12 return actionInvocation. invoke (); 13} else {14 System. out. println ("the Action you accessed is:" + myAction); 15} 16 17 session = ActionContext. getContext (). getSession (); 18 Object User = session. get ("user"); 19 if (user! = Null) {20 return actionInvocation. invoke (); 21} else {22 return "login"; 23} 24 25}

Note: This method can be left blank: <param name = "excludeMethods"> loginView, login </param>

 

② Let it inherit MethodFilterInterceptor:

Public class LoginInterceptor extends MethodFilterInterceptor {private Map <String, Object> session = null; protected String doIntercept (ActionInvocation actionInvocation) throws Exception {/* Object myAction = actionInvocation. getAction (); if (myAction instanceof UserAction) {System. out. println ("the Action you access is UserAction. Do not verify the Session; otherwise, an endless loop"); // release return actionInvocation. invoke ();} else {System. out. prin Tln ("the Action you accessed is:" + myAction);} */session = ActionContext. getContext (). getSession (); Object user = session. get ("user"); if (user! = Null) {return actionInvocation. invoke () ;}else {return "login ";}}}

 

③ UserAction inherits ActionSupport to implement ModelDriven <User> and SessionAware:

1 public class UserAction extends ActionSupport implements ModelDriven <User>, SessionAware {2 3 private Map <String, Object> session = null; 4 private User user = null; 5 // Driver Model 6 public User getModel () {7 this. user = new User (); 8 return this. user; 9} 10 11 public void setSession (Map <String, Object> map) {12 this. session = map; 13} 1415 public String loginView () {16 return "loginViewSuccess"; 17} 18 19 public String login () {20 if ("admin ". equals (user. getUserName () & "123456 ". equals (user. getUserPassword () {21 session. put ("user", user); 22 return this. SUCCESS; 23} else {24 return this. ERROR; 25} 26 27} 28}

 

 

In the Struts. xml file:

<Struts> <package name = "myPackage" extends = "struts-default"> <interceptors> <interceptor name = "loginInterceptor" class = "com. nf. action. loginInterceptor "> </interceptor> <interceptor-stack name =" myStack "> <interceptor-ref name =" loginInterceptor "> <! -- If excludeMethods needs to take effect, the custom Interceptor cannot use the Interceptor interface, but extends MethodFilterInterceptor --> <param name = "excludeMethods"> loginView, login </param> <! -- When this row is not used, we can use the interceptor. --> </interceptor-ref> <interceptor-ref name = "defaultStack"> </interceptor-ref> </interceptor-stack> </interceptors> <! -- Configure a default interceptor, that is, all actions must use --> <default-interceptor-ref name = "myStack"/> <global-results> <result name = "login" type = "redirectAction"> userAction_loginView </result> </global-results> <! -- No method is written. The default value is execute --> <action name = "indexAction" class = "com. nf. action. indexAction "method =" execute "> <result name =" success ">/WEB-INF/jsp/index. jsp </result> <! -- <Interceptor-ref name = "myStack"> </interceptor-ref> --> <! -- Note that this code can be added here, but every action must be put in trouble <interceptor-ref name = "loginInterceptor"> </interceptor-ref> <interceptor-ref name = "defaultStack "> </interceptor-ref> --> </action> <action name =" otherFunctionAction "class =" com. nf. action. otherFunctionAction "> <! -- Do not write name, default is success --> <result>/WEB-INF/jsp/otherFunction. jsp </result> </action> <action name = "userAction _ *" class = "com. nf. action. userAction "method =" {1} "> <result name =" loginViewSuccess ">/WEB-INF/jsp/loginView. jsp </result> <result name = "error">/WEB-INF/jsp/error. jsp </result> <result name = "success" type = "redirectAction"> indexAction </result> <allowed-methods> login, loginView </allowed-methods> </action> </package> </struts>

 

The filter method configured by <param name = "excludeMethods"> loginView and login </param> indicates that the interceptor does not apply to the method. Here, loginView is the method to jump to the logon page.

Login is used to verify the user name and password. In this method, the authenticated user name is put into the session.

  Conclusion: 1. In struts2, all interceptors will inherit the Interceptor interface.

2. If no interceptor is added, struts2 will add the default interceptor for us. Of course, if we specify an interceptor, our own interceptor will replace the default interceptor,

Therefore, we cannot enjoy some features provided by the default interceptor. Therefore, I usually add the default interceptor.

For example, add <interceptor-ref name = "defaultStack"> </interceptor-ref>

 

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.