Java里直接用正则表达式要这样定义
String pattern = "\\w";
因为java里面 \ 本身就是转义字符。
而把表达式放到配置文件里时就要用"\w"了。就因为这个疏忽,多用了两个小时才结束这个Filter。
写个记录,让自己以后少犯这样的错误。
web.xml里的filter配置
<filter> <filter-name>evaluationContextFilter</filter-name> <filter-class> com.gxoa.util.EvaluationContextFilter </filter-class> <init-param> <!-- “on”时为开,其他关闭 --> <param-name>switch</param-name> <param-value>on</param-value> </init-param> <init-param> <!-- 不检查的url,url为正则表达式,范围从小到大排序 --> <param-name>exceptUrls</param-name> <param-value> project!toCreateProject.do$;(function|role)!\w*.do$ </param-value> </init-param> </filter> <filter-mapping> <filter-name>evaluationContextFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>
com.gxoa.util.EvaluationContextFilter
public class EvaluationContextFilter implements Filter{
Logger logger = Logger.getLogger(this.getClass());
private boolean isSwitchOn = false;
private Pattern[] exceptUrlArr;
public void init(FilterConfig config) throws ServletException{
isSwitchOn = "on".equalsIgnoreCase(config.getInitParameter("switch"));
String regs = config.getInitParameter("exceptUrls");
String [] urls = regs.replace("\n", "").replace("\t","").split(";");
exceptUrlArr = new Pattern[urls.length];
for(int i=0;i<urls.length;i++){
exceptUrlArr[i] = Pattern.compile(urls[i].toString());
}
}
}