Requirements: The production of a website requires users to log in to view, that is, a permission problem
1. First, it is clear that there are two action requests that can be passed before the user logs in, i.e. registration and login.
2. Create interceptors, such as Userlogininterceptor.java, as follows
Public classUserlogininterceptorextendsAbstractinterceptor {@Override PublicString Intercept (actioninvocation arg0)throwsException {Action action=(Action) arg0.getaction (); //release both of these action requests if(Actioninstanceofregisteraction| | Actioninstanceofloginaction) { returnArg0.invoke (); }Else{ //get the session there are no users? User user = (user) Arg0.getinvocationcontext (). GetSession (). Get ("User") ; if(user==NULL){ //if not, you want the page to convey the wrong messageArg0.getinvocationcontext (). GetSession (). Put ("NoLand", "You do not have permission, please register or log in first!"). "); return"Input" ; }Else{ //If there is, release it . returnArg0.invoke (); } } }}
3. When the Interceptor is written, it should be configured in Struts.xml
<Interceptors> <!--Configuring a written interceptor - <Interceptorname= "Userlogin"class= "Com.blog.interceptor.UserLoginInterceptor"/> <!--define your own interceptor stack, consisting of Struts2 's own interceptor stack and the front interceptor - <Interceptor-stackname= "Blogstack"> <Interceptor-refname= "Userlogin"/> <Interceptor-refname= "Defaultstack"/> </Interceptor-stack> </Interceptors> <!--To set the custom interceptor stack as the default stack - <Default-interceptor-refname= "Blogstack"/>
4. On the no-access jump page, display the reminder message that you put in the session.
The basic method and operation of Strust2 Custom Interceptor