This example describes the Java custom interceptor and its usage. Share to everyone for your reference. Specifically as follows:
Logininterceptor.java files are as follows:
Package com.tq365.util;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;
Import Org.apache.struts2.ServletActionContext;
Import com.opensymphony.xwork2.ActionInvocation;
Import Com.opensymphony.xwork2.interceptor.AbstractInterceptor;
Import Com.tq365.vo.User;
/** * Custom Interceptor (release login and logged in user operation) * @author archie2010 */public class Logininterceptor extends abstractinterceptor{
Private static final long serialversionuid = 1406123004582563032L; @Override public String Intercept (actioninvocation invocation) throws Exception {HttpServletRequest request = SERVL
Etactioncontext.getrequest ();
HttpSession session = Servletactioncontext.getrequest (). GetSession ();
User user = (user) Session.getattribute ("User");
The requested URL String Path = Request.getservletpath ();
Boolean flag = false;
if ("/loginuser.jspx". Equals (path)) {flag = true;
}else{if (user!=null) {flag = true; }} System.out.println(path); Return flag?
Invocation.invoke (): "Error";
}
}
Struts.xml files are as follows:
<interceptors>
<!--login Interceptor-->
<interceptor name= "Login" class= " Com.tq365.util.LongInterceptor "/>
<interceptor-stack name=" Myinterceptor ">
<interceptor-ref Name= "Login"/>
<interceptor-ref name= "Paramsprepareparamsstack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name= "Myinterceptor"/>
I hope this article will help you with your Java programming.