如何更改HttpServletRequest的“pathInfo”

时间:2011-02-23 14:14:53

标签: java spring spring-mvc httprequest

我害怕提出一个奇怪的问题,但我想改变" pathInfo"在Controller的处理程序方法中的HttpServletRequest。请看下面的内容。

我知道我可以获得" pathInfo"通过使用getPathInfo()。然而。我不知道如何设置pathInfo。可能吗 ?任何帮助将不胜感激

@RequestMapping(value = "show1" method = RequestMethod.GET)
public String show1(Model model, HttpServletRequest request) {

    // I want to set up "PathInfo" but this kind of methods are not provided 
    //request.setPathInfo("/show2");

    // I thought that BeanUtils.copy may be available.. but no ideas.

    // I have to call show2() with the same request object
    return show2(model, request);
}

// I am not allowed to edit this method
private String show2(Model model, HttpServletRequest request) {

    // I hope to display "http://localhost:8080/contextroot/show2"
    System.out.println(request.getRequestURL());

    return "complete";
}

2 个答案:

答案 0 :(得分:4)

您无法设置这些值。

唯一的选择是为您的请求创建一个包装器,如下所示:

return show2(model, new HttpServletRequestWrapper(request) {
    public StringBuffer getRequestURL() {
        return new StringBuffer(
            super.getRequestURL().toString().replaceFirst("/show1$", "/show2"));
    }
});

答案 1 :(得分:1)

路径信息由浏览器(客户端)在请求某个URL时设置。