struts2 no extension(excludePattern)


使用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="/compressor?v=${globalVersion}&type=css&munge=true&files=/cssStyle/index.css,/cssStyle/dialog.css,/cssStyle/jbox/Gray/jbox.css,/cssStyle/home.css">
<script src="/compressor?v=${globalVersion}&type=js&munge=true&files=
/javascript/lib/json2/json2.js,
/javascript/lib/jquery/1.7.2/jquery.js,
/javascript/lib/jquery/jquery.ext.js,
/javascript/lib/juicer/0.6.1/juicer-min.js,
/javascript/lib/underscore/1.3.3/underscore-min.js,
/javascript/lib/cookie/cookie.min.js,
/javascript/core/core.js,
/javascript/core/toptips.js,
/javascript/core/selectBankCard.js,
/javascript/core/dialog.js,
/javascript/core/page.js,
/javascript/core/regex.js,
/javascript/core/topLogRegister.js"
type="text/javascript"></script>

即使用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  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值