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

时间:2023-09-18 17:04:50

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