113

クライアントがアクティブなリクエストに使用する URL に基づいて URL を作成したいと考えています。HttpServletRequest現在のオブジェクトを取得するよりもスマートなものはありますか?それは、getParameter...()GET パラメーターを含む (および唯一の) 完全な URL を再構築するメソッドです。

明確化: 可能であれば、HttpServletRequestオブジェクトの使用を辞退したいと考えています。

4

7 に答える 7

125

Well there are two methods to access this data easier, but the interface doesn't offer the possibility to get the whole URL with one call. You have to build it manually:

public static String makeUrl(HttpServletRequest request)
{
    return request.getRequestURL().toString() + "?" + request.getQueryString();
}

I don't know about a way to do this with any Spring MVC facilities.

If you want to access the current Request without passing it everywhere you will have to add a listener in the web.xml:

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

And then use this to get the request bound to the current Thread:

((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
于 2009-09-29T06:58:24.800 に答える
8

jsp ファイル内:

request.getAttribute("javax.servlet.forward.request_uri")
于 2013-03-18T08:12:24.717 に答える