mar*_*osh 6 controller spring-mvc
我有工作的servlet需要转换为Spring MVC控制器才能访问spring bean等.为什么在普通的servlet中request.getPathInfo()没有返回null,但在Spring Controller中我得到null值?我知道我可以使用@PathVariable,但想知道为什么这种方法的结果有区别?
@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...
}
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*nto 10
我认为解决方案是在getPathInfo()的javadoc中
额外的路径信息在servlet路径之后但在查询字符串之前,并以"/"字符开头.
在Spring的情况下,servlet路径是完整路径,因此如果你调用getServletPath(),它将始终返回完整的URI,而getPathInfo()将不返回任何内容.
| 归档时间: |
|
| 查看次数: |
3124 次 |
| 最近记录: |