码迷,mamicode.com
首页 > Web开发 > 详细

struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)

时间:2016-02-02 18:57:04      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

一、web.xml配置

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>com.appdept.servlet.MyStrutsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

二、自定义拦截器

public class MyStrutsFilter extends StrutsPrepareAndExecuteFilter {

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String url = request.getRequestURI();
if (url.contains("PDFServer.jsp")) {
/*这两句在我的项目中是为了防止乱码,可以不用管
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");*/
chain.doFilter(req, res);  //直接跳过
}else{
/*req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");*/
super.doFilter(req, res, chain); //chain继续进下一个拦截器
}
}
}

struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)

标签:

原文地址:http://www.cnblogs.com/-blog/p/5178144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!