2011-11-10 49 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

現在肯定應該把它標記爲正確的答案。 –