首页 > 代码库 > struts2 no extension

struts2 no extension


使用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 <tt>true</tt> if the request URI matches one of the given patterns
     */
    public boolean isUrlExcluded( HttpServletRequest request, List<Pattern> 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了,如果想兼容之前的,可以添加上之间的,如:  
<pre name="code" class="plain">struts.action.extension=<span style="font-family: Arial, Helvetica, sans-serif;">do,action,jspt,, </span>

另外,如果你使用的struts2的版本低于2.3.16(至于具体那个版本是过度的,我没有测试),静态资源js,css可能会被吃掉了,可以添加如下的一个属性

struts.action.excludePattern=/css,/javascript
有的项目中处理js和css的方式是对js、css进行压缩的servlet,如:

<link type="text/css" rel="stylesheet" href=http://www.mamicode.com/"/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

注意:在设置struts2的配置时,最好使用struts.properties,因为我在struts.xml中使用相同的配置,就不兼容以前的".do"了,如果struts.properties和struts.xml同时存在,struts.properties的优先级会高于struts.xml  

struts2 no extension