在Spring MVC中获取当前URL的最佳方法是什么?

我想基于客户端用于活动请求的URL创建URL。有什么比获取当前HttpServletRequest对象更聪明的getParameter...()方法吗?它是重建完整URL

的方法,包括(仅)GET参数。

回答:

好了,有两种方法可以更轻松地访问此数据,但是该接口无法通过一个调用来获取整个URL。您必须手动构建它:

public static String makeUrl(HttpServletRequest request)

{

return request.getRequestURL().toString() + "?" + request.getQueryString();

}

我不知道如何使用任何Spring MVC工具来做到这一点。

如果要访问当前请求而不将其传递到任何地方,则必须在web.xml中添加一个侦听器:

<listener>

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

</listener>

然后使用它来获取绑定到当前线程的请求:

((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()

以上是 在Spring MVC中获取当前URL的最佳方法是什么? 的全部内容, 来源链接: utcz.com/qa/423474.html

回到顶部