Struts2与DWR在项目中的共存解决方案
1.struts2和dwr为什么会冲突?
最近很多同学都在问同样一个问题,struts2和dwr该如何共存?我们先分析一下为什么会出现冲突.
struts2通过StrutsPrepareAndExecuteFilter过滤器来处理请求,常见的配置如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
问题就在这里,<url-pattern>/*</url-pattern>配置过滤了所有的请求,当用户提交的请求是/dwr/*时,也同样被StrutsPrepareAndExecuteFilter处理了,根本达不到DWR框架,这就是为什么二者不能共存的原因.
2.通过struts2源码发现问题
网上我没找到问题的解决方案,所以,只能通过阅读源代码来查找问题的根源了.在StrutsPrepareAndExecuteFilter类中,重写了doFilter方法:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
prepare.setEncodingAndLocale(request, response);
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}
}
} finally {
prepare.cleanupRequest(request);
}
}
注意看粗体部分,如果excludedPatterns不为null且prepare.isUrlExcluded方法的返回值为true,则该请求不交给struts框架处理,那么,sUrlExcluded是如何定义的呢?我们跟踪找到下面的代码:
public boolean isUrlExcluded( HttpServletRequest request, List<Pattern> excludedPatterns ) {
if (excludedPatterns != null) {
String uri = getUri(request);
for ( Pattern pattern : excludedPatterns ) {
if (pattern.matcher(uri).matches()) {
return true;
}
}
}
return false;
}
我们看到, isUrlExcluded 方法中 excludedPatterns参数是一个List集合, String uri = getUri(request);代码获取请求路径,然后通过一个for循环将请求路径与 excludedPatterns进行匹(,注意,这里是通过正则表达式进行匹配的),如果匹配成功,则返回true,否则返回false.
那么, excludedPatterns的值从哪里来呢?下面的代码是也:
public List<Pattern> buildExcludedPatternsList( Dispatcher dispatcher ) {
return buildExcludedPatternsList(dispatcher.getContainer().getInstance(String.class, StrutsConstants.STRUTS_ACTION_EXCLUDE_PATTERN));
}
private List<Pattern> buildExcludedPatternsList( String patterns ) {
if (null != patterns && patterns.trim().length() != 0) {
List<Pattern> list = new ArrayList<Pattern>();
String[] tokens = patterns.split(",");
for ( String token : tokens ) {
list.add(Pattern.compile(token.trim()));
}
return Collections.unmodifiableList(list);
} else {
return null;
}
}
看了上面的代码,是不是豁然开朗?原来, StrutsConstants.STRUTS_ACTION_EXCLUDE_PATTERN常量保存了不由struts2处理的路径,我们在struts2.properties中配置即可.
struts.action.excludePattern=/dwr/.*,/dwr/test/.*