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

一、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继续进下一个拦截器
}
}
}

转载于:https://www.cnblogs.com/-blog/p/5178144.html

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页