2011-11-10 51 views
5

我已经工作的servlet需要转换为Spring MVC控制器来访问弹簧bean等。为什么在普通servlet request.getPathInfo()返回不是null,但在Spring Controller中我得到空值?我知道我可以使用@PathVariable,但不知道为什么这种方法的结果是差异?Spring MVC控制器 - getPathInfo()为空

@RequestMapping(value = {"/test", "/test/*"}) 
public void test(HttpServletRequest req, HttpServletResponse res) { 

    log.info(req.getPathInfo() == null); // true! 

    if (req.getMethod().equalsIgnoreCase("get")) { 
     // analogue to doGet... 
    } else { 
     // analogue to doPost... 
    } 

} 
+0

你打的网址是什么? –

+0

URL:'http:// localhost:8080/myApp/test/hello'。 'req.getPathInfo()'shuld return'/ hello' – marioosh

回答

6

我认为解决的办法是在getPathInfo的javadoc的()

额外的路径信息如下servlet的路径,但先于 查询字符串,并以“/”字符开头。

在Spring的情况下,servlet路径是完整路径,因此如果你调用getServletPath(),它会总是返回完整的URI和getPathInfo()将返回什么。

+0

现在肯定应该把它标记为正确的答案。 –