Struts2 interceptor issue for fixing sign of _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

Hi learning network

FIG interceptor works by interceptors each action request (request) are wrapped inside a series of interceptor, by sending a request redirectAction again.

Interceptor can perform linear operations may be made similar to do after the recovery operation performed directly in the Action in the Action.

We can make each Action can either be transferred to the following operational interceptors, Action can be directly returned to the client quit the operation established screen.

Then how do we define an interceptor:

A custom interceptor as follows:

1, or implement Interceptor inherit AbstractInterceptor abstract class.

2, create a file Struts.xml define interceptors.

3, the above-defined reference Action interceptors need to use, for convenience may be defined as the default interceptor interceptor (<default-interceptor-ref name = "myStack" />),

Thus, without adding a special statements are all Action interceptor intercepting <param name = "excludeMethods"> loginView, login </ param>.

①Interceptor interface declares three methods:

public class LoginInterceptor implements Interceptor {

 private Map<String,Object> session = null;
 public void destroy() { }
 public void init() { }
 public String intercept(ActionInvocation actionInvocation) throws Exception {
 8     Object myAction = actionInvocation.getAction();
  if(myAction instanceof UserAction){
   System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
   //放行
   return actionInvocation.invoke();
  }else{
   System.out.println("你访问的Action是:"+myAction);
  }

  session = ActionContext.getContext().getSession();
  Object user = session.get("user");
  if (user!=null){
   return actionInvocation.invoke();
  the else {} 
   return "Login"; 
  } 

} 

Note: This method can not add: <param name = "excludeMethods" > loginView, login </ param>

② inherited 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("你访问的Action是UserAction,不要校验Session,否则死循环");
   //放行
   return actionInvocation.invoke();
  }else{
   System.out.println("你访问的Action是:"+myAction);
  }
  */
  session = ActionContext.getContext().getSession();
  Object user = session.get("user");
  if (user!=null){
   return actionInvocation.invoke();
  }else{
   return "login";
  }
 }
}

③UserAction inheritance ActionSupport achieve ModelDriven <User> and SessionAware:

public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware{

 private Map<String,Object> session = null;
 private User user = null;
   //驱动模型
 public User getModel() {
  this.user = new User();
  return this.user;
 }

 public void setSession(Map<String, Object> map) {
  this.session = map;
 }

 public String loginView(){
  return "loginViewSuccess";
 }

 public String login(){
  if ("admin".equals(user.getUserName())&&"123456".equals(user.getUserPassword())){
   session.put("user",user);
   return this.SUCCESS;
  }else{
   return this.ERROR;
  }

 }
}

Struts.xml file:

<Struts> 
 <Package name = "the myPackage" the extends = "Struts-default"> 

  <interceptors> 

   <Interceptor name = "loginInterceptor" class = "com.nf.action.LoginInterceptor"> </ Interceptor> 
   <name = Stack-Interceptor "myStack"> 
    <interceptor-ref name = "loginInterceptor"> 
     <-! excludeMethods to be in effect if a custom interceptors can not be used implement interceptor, but MethodFilterInterceptor the extends -> 
     <param name = "excludeMethods"> loginView, login </ param> < -! this line is we can not use interceptors with ① -> 
    </ interceptor-ref> 
    <ref name =-interceptor "defaultStack"> </ interceptor-ref> 
   </ interceptor the --stack> 
  </ interceptors> 
  <-! configure a default interceptor, that is, all of the Action must use the -> 
  <default-Interceptor-ref name = "myStack" /> 
  <-Results, Ltd. Free Join>
   <the Result name = "the Login" of the type = "redirectAction"> userAction_loginView </ the Result> 
  </, Ltd. Free Join-Results> 
  <-! do not write method, the default is the Execute -> 
  <Action name = "the indexAction" class = "COM. nf.action.IndexAction "Method =" Execute "> 
   <name = Result" Success "> / the WEB-INF / JSP / the index.jsp </ Result> 
   <-! 
   <REF-Interceptor name =" myStack "> </ ref-Interceptor> 
   -> 
   <-! comment here you can put the code for each action should be put just too much trouble 
   <Interceptor-ref name = "loginInterceptor"> </ Interceptor-ref> 
   <ref name =-Interceptor "defaultStack"> </ Interceptor-REF> 
   -> 
  </ Action>

  <action name="otherFunctionAction" class="com.nf.action.OtherFunctionAction">
   <!--不写name,默认就是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>

Wherein the filtering method <param name = "excludeMethods"> loginView, login </ param> configuration, meaning that a method wherein the interceptor does not work. I am here, loginView is a method to jump to the login page.

login user name and password authentication is a method, which will be verified in the user name into the session.

to sum up:

1. In struts2, all Interceptor interceptor will inherit this interface.

2. If we do not add interceptors, struts2 will add to our default interceptors. Of course, if we specify interceptors, our own interceptors will replace the default interceptors,

So we can not enjoy some of the features provided by default interceptors. So, in general I would also add default interceptors.

For example, in the above configuration item, action which together with <interceptor-ref name = "defaultStack"> </ interceptor-ref>

Above this Struts2 interceptor logged on to solve the problem is the small series to share the entire contents of everyone, and I hope to give you a reference, I hope you will support sensitive and eager Forum / Hi learning network.

The original address is: http: //www.piaodoo.com/thread-13252-1-1.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093789.html