2011-02-23 27 views
2

我害怕问一个奇怪的问题,但我想在Controller的处理程序方法中更改HttpServletRequest的“pathInfo”。请看下面。如何更改HttpServletRequest的“pathInfo”

我知道我可以通过使用getPathInfo()来获得“pathInfo”。然而。我不知道如何设置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"; 
} 
+0

你为什么要这么做?首先,我认为这不是一个好主意。 – adarshr 2011-02-23 14:19:38

+0

@adarshr感谢您的评论。我确实知道这是一种不好的方法,但我必须在我的工作中采用一种不可修改的方法。 – zono 2011-02-23 14:52:31

回答

4

您不能设置这些值。

唯一的选择是创建一个包装您的要求,这样的事情:

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

哦!谢谢!它没有任何修改的show2()!但为什么?我从来没有见过这样的符号......这是否意味着getRequestURL()被替换? – zono 2011-02-23 14:35:39

+0

和任何人。我想深刻地知道这个符号。你能告诉我叫什么名字吗?我会谷歌。 – zono 2011-02-23 14:41:59

+0

@yusaku:它是一个匿名的内部类,它正在创建,它扩展了'HttpServletRequestWrapper'并覆盖了它的一个方法。 'HttpServletRequestWrapper'用于方便地实现'HttpServletRequest'的代理模式。 – axtavt 2011-02-23 14:49:12

1

路径信息是由浏览器(客户端)设置时,它请求一个特定的URL。

+0

非常感谢。嗯......我明白我明白了。 – zono 2011-02-23 14:38:24

相关问题