论坛首页 Java版 Webwork

WW的RequestUtils为什么这样写?

浏览 2691 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2006-03-25
webwork 2.2.2 + Tomcat 5.5.16

com.opensymphony.webwork.RequestUtils

[code:1]    public static String getServletPath(HttpServletRequest request) {
        String servletPath = request.getServletPath();
       
        if (null != servletPath && !"".equals(servletPath)) {
            return servletPath;
        }
       
        String requestUri = request.getRequestURI();
        int startIndex = request.getContextPath().equals("") ? 0 :                                   
                                      request.getContextPath().length();
        int endIndex = request.getPathInfo() == null ? 
                                      requestUri.length() :
                                requestUri.lastIndexOf(request.getPathInfo());
       
        if (startIndex > endIndex) { // this should not happen
            endIndex = startIndex;
        }
       
        return requestUri.substring(startIndex, endIndex);
    }[/code:1]

当web.xml里面mapping url pattern设成  *.action 的时候,通过 request.getServletPath() 就能取到完整的 servlet path,而当 url pattern 设成 /* 的时候,request.getServletPath()返回的是empty,并且后面的取子串的代码最终返回的也是empty。

我觉得那个算endIndex的表达式可能错了。
   
最后更新时间:2006-03-26
问题是你什么地方要用它的这个RequestUtils哪? 用它来做什么哪
   
0 请登录后投票
最后更新时间:2006-03-26
scud 写道
问题是你什么地方要用它的这个RequestUtils哪? 用它来做什么哪

咳,人家研究代码不行么,还是回答别人的问题是正道
弄的我们新人都不敢发问了
   
0 请登录后投票
最后更新时间:2006-03-26
我使用 RestfulActionMapper 的时候,由于这个mapper不支持*.action模式的匹配,所以web.xml那里一定要写成 /foo/* 的形式。结果产生找不到action的错误,经过调试发现问题在这里。

另外今天又比较了一下 DefaultActionMapper 的代码

[code:1]    String getUri(HttpServletRequest request) {
        // handle http dispatcher includes.
        String uri = (String) request.getAttribute("javax.servlet.include.servlet_path");
        if (uri != null) {
            return uri;
        }

        uri = RequestUtils.getServletPath(request);
        if (uri != null && !"".equals(uri)) {
            return uri;
        }

        uri = request.getRequestURI();
        return uri.substring(request.getContextPath().length());
    }[/code:1]

DefaultActionMapper并没有用到那个方法,而是自己内部处理的,我觉得DefaultActionMapper这个方法才是对的。
   
0 请登录后投票
最后更新时间:2006-03-26
我试了一下 ,使用的是webwork 2.2.2带的webapp下面的blank

我修改了 webwork.properties,
webwork.mapper.class=com.opensymphony.webwork.dispatcher.mapper.RestfulActionMapper

web.xml没有变化

访问
http://localhost:8300/testww222/home/aa/bb

正常运行

在webwork 2.2.2中是默认的例子是不映射到.action的,web.xml默认的配置可以参考blank项目里面的web.xml

不知道你是怎么设置的。按照我上面说的,没有出现什么问题
   
0 请登录后投票
最后更新时间:2006-03-27
OMG,我使用了过期的地图,我同时加载了FilterDispatcher和ServletDispatcher……

不过我还是认为那段代码的后半截有bug,用FilterDispatcher得到正确结果是因为通过request.getServletPath()已经取到了值,直接return了,后半截代码没有被执行。
   
0 请登录后投票
最后更新时间:2006-03-27
做技术要严谨,不要轻易下结论

如果你认为它有bug,那么拿出实际的测试用例才是最有说服性的 
   
0 请登录后投票
最后更新时间:2006-03-27
昨晚经你提醒以后我试过了,如果在web.xml中使用ServletDispatcher,映射方式是 /* ,并且 ActionMapper 用的是 RestfulActionMapper 就可以运行到后半截代码,然后返回的肯定是 "", 也就找不到对应的action了。
   
0 请登录后投票
论坛首页 Java版 Webwork

跳转论坛:
JavaEye推荐