(转)struts1 也能使用拦截器
我用struts1用习惯了。不想换S2 可是我比较喜欢S2的拦截器。所以就想了下怎么让s1也能拥有拦截器。做的不好别喷我 我第一次写博客!
废话就不多说了,我直接进正题!
1·首先写一个类 MyInterceptor 继承ActionServlet,代码如下
public class MyInterceptor extends ActionServlet {
?@Override
?public void doPost(HttpServletRequest request, HttpServletResponse response)
???throws IOException, ServletException {
??// TODO Auto-generated method stub
??System.out.println("dopost");//这里可以写成自己的拦截代码。。大家就随意发挥吧
??super.doPost(request, response);
?}
?@Override
?public void doGet(HttpServletRequest request, HttpServletResponse response)
???throws IOException, ServletException {
??// TODO Auto-generated method stub
??System.out.println("doget");//这里可以写成自己的拦截代码。。大家就随意发挥吧
??super.doGet(request, response);
?}
}
2·然后把WEB.xml改下下面这样?
? <servlet>
??? <servlet-name>action</servlet-name>
??? <servlet-class>org.apache.struts.action.MyInterceptor.MyInterceptor</servlet-class>
??? <init-param>
????? <param-name>config</param-name>
????? <param-value>/WEB-INF/struts-config.xml</param-value>
??? </init-param>
??? <init-param>
????? <param-name>debug</param-name>
????? <param-value>3</param-value>
??? </init-param>
??? <init-param>
????? <param-name>detail</param-name>
????? <param-value>3</param-value>
??? </init-param>
??? <load-on-startup>0</load-on-startup>
? </servlet>
? <servlet-mapping>
??? <servlet-name>action</servlet-name>
??? <url-pattern>*.do</url-pattern>
? </servlet-mapping>
?