在Spring MVC中获取根/基本URL
问题内容:
在Spring MVC中获取Web应用程序根目录/基础URL的最佳方法是什么?
基本网址=
http://www.example.com或http://www.example.com/VirtualDirectory
问题答案:
如果基本网址是“ http://www.example.com ”,则使用以下内容获取“
www.example.com ”部分,而不使用“ http://”:
从控制器:
@RequestMapping(value = "/someURL", method = RequestMethod.GET)
public ModelAndView doSomething(HttpServletRequest request) throws IOException{
//Try this:
request.getLocalName();
// or this
request.getLocalAddr();
}
从JSP:
在文档顶部声明以下内容:
<c:set var="baseURL" value="${pageContext.request.localName}"/> //or ".localAddr"
然后,要使用它,请引用变量:
<a href="http://${baseURL}">Go Home</a>