這一章,我們開始struts2中攔截器的學習。
自定義攔截器
一、增加一個自定義的攔截器為類
package com.huhx.interceptor;import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class RegisterInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L;@Overridepublic String intercept(ActionInvocation invocation) throws Exception {System.out.println("攔截器開始運行....");String resultString = invocation.invoke();System.out.println("攔截器結束運行....");return resultString;} }
?二、修改struts.xml配置文件,內容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><package name="default" namespace="/" extends="struts-default"><interceptors><interceptor name="registerInterceptor" class="com.huhx.interceptor.RegisterInterceptor"></interceptor></interceptors><action name="hello" class="com.huhx.struts.HuhxAction"><result name="success">/huhx.jsp</result><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="registerInterceptor"></interceptor-ref></action></package> </struts>
?三、訪問地址:http://localhost:8080/StrutsTest1/hello.action,后臺打印地址:
攔截器開始運行....
hello world.
攔截器結束運行....
設置攔截還能打進來,?
?友情鏈接
?