struts2 no extension(excludePattern)
阅读原文时间:2023年07月16日阅读:1

采用struts2 小伙伴非常希望更改或删除action扩展,本文将帮助你实现

struts2-core-2.3.16.jar , 下载链接: http://repo1.maven.org/maven2/org/apache/struts/struts2-core/2.3.16/

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter , 这个struts2处理请求的函数doFilter 。 在这里使用了 prepare.isUrlExcluded来推断是否排除的请求。假设是就直接运行chain.doFilter(request, response);交给其它的Filter处理,否则自己处理此action

//… // protected PrepareOperations prepare;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;  
    HttpServletResponse response = (HttpServletResponse) res;

    try {  
        if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { //看这里  
            chain.doFilter(request, response);  
        } else {  
            prepare.setEncodingAndLocale(request, response);  
            prepare.createActionContext(request, response);  
            prepare.assignDispatcherToThread();  
            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);  
    }  
}  

//…

org.apache.struts2.dispatcher.ng.PrepareOperations

/**
* Check whether the request matches a list of exclude patterns.
*
* @param request The request to check patterns against
* @param excludedPatterns list of patterns for exclusion
*
* @return true if the request URI matches one of the given patterns
*/
public boolean isUrlExcluded( HttpServletRequest request, List excludedPatterns ) {
if (excludedPatterns != null) {
String uri = RequestUtils.getUri(request);
for ( Pattern pattern : excludedPatterns ) {
if (pattern.matcher(uri).matches()) {
return true;
}
}
}
return false;
}

经过以上的分析。如今在看看struts2的默认配置文件default.properties,在struts2-core-2.3.16.jar,  org.apache.truts2以下

### Used by the DefaultActionMapper

You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do

The blank extension allows you to match directory listings as well as pure action names

without interfering with static resources, which can be specified as an empty string

prior to a comma e.g. struts.action.extension=, or struts.action.extension=x,y,z,,

struts.action.extension=action,,

依据描写叙述。能够设置struts.action.extension的value为一个逗号就能够支持无扩展名的action了。假设想兼容之前的,能够加入上之间的,如:  

struts.action.extension=do,action,jspt,, 

另外。假设你使用的struts2的版本号低于2.3.16(至于详细那个版本号是过度的。我没有測试),静态资源js,css可能会被吃掉了,能够加入例如以下的一个属性

struts.action.excludePattern=/css,/javascript

有的项目中处理js和css的方式是对js、css进行压缩的servlet,如:

<link type="text/css" rel="stylesheet" href="/compressor?

v=${globalVersion}&type=css&munge=true&files=/cssStyle/index.css,/cssStyle/dialog.css,/cssStyle/jbox/Gray/jbox.css,/cssStyle/home.css">

即使用servlet  compressor 输出js和css文件,此时配置应例如以下

struts.action.excludePattern=/compressor

如要过滤 以 “/druid”和“/compressor”开头的请求,模式例如以下(注意加入的是".*"。而不仅仅是“*”)

struts.action.excludePattern=/compressor.*,/druid.*

注意:在设置struts2的配置时。最好使用struts.properties,由于我在struts.xml中使用同样的配置,就不兼容曾经的".do"了。假设struts.properties和struts.xml同一时候存在,struts.properties的优先级会高于struts.xml

版权声明:本文博主原创文章,博客,未经同意不得转载。