簡體   English   中英

如何更改HttpServletRequest的“ pathInfo”

[英]How to change “pathInfo” of HttpServletRequest

我怕問一個奇怪的問題,但是我想在Controller的處理程序方法上更改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";
}

您無法設置這些值。

唯一的選擇是為您的請求創建包裝,如下所示:

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

路徑信息由瀏覽器(客戶端)在請求某個URL時設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM