Struts2 Study Notes (10) -- custom interceptor and struts2 Study Notes
The Struts2 interceptor is implemented based on the idea of AOP, while the implementation of AOP is based on dynamic proxy. The Struts2 interceptor intercepts an Action before or after it is accessed, and the Struts2 interceptor is pluggable. The Struts2 interceptor stack is a chain that connects the interceptor in sequence, when the interception requirements are met, the interceptor will be executed in sequence according to the implementation declaration.
1. Introduction to Struts2 custom interceptor
All interceptors of Struts2 must implement the Interceptor interface. The Interceptor interface mainly has three methods:
- Init (): initialization method, which is executed only once when the interceptor is loaded.
- Intercept (ActionInvocation invocation): the method used by the Interceptor to execute each request.
- Destory (): Destroy method, which is executed only once when the interceptor is released.
The AbstractInterceptor abstract class implements the Interceptor interface. It also provides a blank implementation for init () and destroy (). Therefore, in actual development, the custom interceptor only needs to inherit the AbstractInterceptor class and implement intercept (ActionInvocation invocation) method.
2. Create a Struts2 interceptor
1) create a class to implement the Interceptor interface or inherit the AbstractInterceptor class.
2) override the intercept method. This method is used to really intercept operations. To continue accessing other interceptors, you must call the invoke method through the ActionInvocation parameter in the intercept method.
3) configure the interceptor in the struts. xml file. There are two scenarios:
- To configure the shared interceptor for all <action> in the package, you must set <default-interceptor-ref> to set the default interceptor. You must configure it before <action>.
- If no interceptor stack is configured, only the interceptor set by <default-interceptor-ref> is executed.
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 </interceptors> 5 <default-interceptor-ref name="login"></default-interceptor-ref> 6 <action name="loginAction" class="com.sunny.action.LoginAction"> 7 <result>/success.jsp</result> 8 <result name="error">/error.jsp</result> 9 </action>10 </package>
- Configure interceptor Stack
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 <interceptor-stack name="my"> 5 <interceptor-ref name="login"></interceptor-ref> 6 <interceptor-ref name="defaultStack"></interceptor-ref> 7 </interceptor-stack> 8 </interceptors> 9 <default-interceptor-ref name="my"></default-interceptor-ref>10 <action name="loginAction" class="com.sunny.action.LoginAction">11 <result>/success.jsp</result>12 <result name="error">/error.jsp</result>13 </action>14 </package>
- To configure an interceptor for a <action> under a package, you must set the interceptor in <action> by setting <interceptor-ref>. To execute defaultStack, you must configure it in <action>.
- No interceptor stack is configured
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 </interceptors> 5 <action name="loginAction" class="com.sunny.action.LoginAction"> 6 <result>/success.jsp</result> 7 <result name="error">/error.jsp</result> 8 <interceptor-ref name="login"></interceptor-ref> 9 <interceptor-ref name="defaultStack"></interceptor-ref>10 </action>11 </package>
- Configure interceptor Stack
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 <interceptor-stack name="my"> 5 <interceptor-ref name="login"></interceptor-ref> 6 <interceptor-ref name="defaultStack"></interceptor-ref> 7 </interceptor-stack> 8 </interceptors> 9 <action name="loginAction" class="com.sunny.action.LoginAction">10 <result>/success.jsp</result>11 <result name="error">/error.jsp</result>12 <interceptor-ref name="my"></interceptor-ref>13 </action>14 </package>
2. Implementation example of Struts2 custom Interceptor: Determine whether to log on
This example is mainly used to verify whether the user is logged on. If the user is not logged on, the error. jsp page is displayed, prompting you to log on to the system.
Interceptor class:
1 public class LoginIntercept extends AbstractInterceptor { 2 3 @Override 4 public String intercept(ActionInvocation invocation) throws Exception { 5 Map session = ServletActionContext.getContext().getSession(); 6 if (session.get("user")==null) { 7 return "error"; 8 } else { 9 return invocation.invoke();10 }11 }12 }
Action class:
1 public class LoginAction extends ActionSupport { 2 private String name; 3 public String getName() { 4 return name; 5 } 6 public void setName(String name) { 7 this.name = name; 8 } 9 @Override10 public String execute() throws Exception {11 return "success";12 }13 }
Struts. xml configuration file:
1 <struts> 2 <constant name="struts.devMode" value="true" /> 3 4 <package name="default" namespace="/" extends="struts-default"> 5 <interceptors> 6 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 7 <interceptor-stack name="my"> 8 <interceptor-ref name="login"></interceptor-ref> 9 <interceptor-ref name="defaultStack"></interceptor-ref>10 </interceptor-stack>11 </interceptors>12 <action name="loginAction" class="com.sunny.action.LoginAction">13 <result>/success.jsp</result>14 <result name="error">/error.jsp</result>15 <interceptor-ref name="my"></interceptor-ref>16 </action>17 </package>18 </struts>
Input. jsp page:
1 <body> 2 <form action = "$ {pageContext. servletContext. contextPath}/loginAction. action "> 3 name: <input type = "text" name = "name"> <br> 4 <input type = "submit" value = "submit"> 5 </form> 6 </body>
Error. jsp page:
1 <body> 2 log on to the system 3 </body>
Logon interface:
Because you have not logged on to the system, clicking submit will display: