User Login Page login.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% request.getsession (). SetAttribute ("User", "has logined"); %> user is already logged in!
To customize the interceptor you need to implement the Com.opensymphony.xwork2.interceptor.Interceptor interface
Package Struts2.example.interceptor;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actioninvocation;import Com.opensymphony.xwork2.interceptor.interceptor;public Class Permissioninterceptor implements Interceptor {//the method will be executed when the class is destroyed @overridepublic void Destroy () {//TODO auto-generated Method stub}//instantiates an interceptor when STRUTS2 is started @overridepublic void init () {//TODO auto-generated method stub}// This method is executed when the action is intercepted @overridepublic String intercept (actioninvocation invocation) throws Exception {//First determine if the user is logged on to object User=actioncontext.getcontext (). GetSession (). Get ("user"), if (user!=null) {//user Not empty, indicates that the user is logged in return Invocation.invoke ();//The method to which the method is intercepted can execute}actioncontext.getcontext (). Put ("message", "You do not have permission to perform the operation"); return "success ";}}
Login Results Page showinfo.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apa Che.org/dtds/struts-2.0.dtd "><struts><constant name=" struts.multipart.maxSize "value=" 10701096 "/> <package name= "csdn" namespace= "/control" extends= "Struts-default" ><interceptors><interceptor name= "Permissioninterceptor" class= "Struts2.example.interceptor.PermissionInterceptor"/><interceptor-stack name = "Permissioninterceptorstack" ><interceptor-ref name= "Defaultstack"/><interceptor-ref name= " Permissioninterceptor "/></interceptor-stack></interceptors><global-results><result Name = "Success" >/web-inf/page/showinfo.jsp</result></global-results><action name= "list_*" class= " Struts2.example.action.FilterAction "method=" {1} "><interceptor-ref name=" Permissioninterceptorstack "/> </action></package></struts>
After registering a custom interceptor in action, struts2 many of the default interceptors will be blocked and will not be able to use struts2 many of the default features. To solve this problem, you can define the interceptor stack. In the interceptor stack, the definition is executed before the previous execution, after the definition.
If you want all the action in the package to be available with this interceptor, you can use <default-interceptor-ref name= "Permissionstack"/>
If you want an interceptor to be specific to an action, and you don't want the action to lose some of the struts2 default blocking capabilities, you can define multiple interceptors under that action.
Specific information about Defaultstack can be viewed in struts-default.xml: <interceptor-stack name= "Defaultstack" > <interceptor-ref name= "Exception"/> <interceptor-ref name= "Alias"/> <interceptor-ref name= "ServletConfig"/> <interceptor-ref name= "i18n"/> <interceptor-ref name= "Prepare"/> <interceptor -ref name= "Chain"/> <interceptor-ref name= "debugging"/> <interceptor-ref name= "s Copedmodeldriven "/> <interceptor-ref name=" Modeldriven "/> <interceptor-ref name= "FileUpload"/> <interceptor-ref name= "checkbox"/> <interceptor-ref name= "multise Lect "/> <interceptor-ref name=" Staticparams "/> <interceptor-ref name=" Actionmapp Ingparams "/> <interceptor-ref name=" params "> <param name=" excludeparams ">do Jo\. *,^struts\. *</param> </interceptor-ref> <interceptor-ref name= "Conversionerror"/> <interceptor-ref name= "Validation" > <param name= "excludemethods" >input,back,cancel,bro wse</param> </interceptor-ref> <interceptor-ref name= "Workflow" > <param name= "Excludemethods" >input,back,cancel,browse</param> </interceptor-ref> ; </interceptor-stack>
Struts2 's Custom Interceptor