在Spring MVC中获取根/基本URL [英] Get Root/Base Url In Spring MVC
本文介绍了在Spring MVC中获取根/基本URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Spring MVC中获取Web应用程序的根/基URL的最佳方法是什么?
What is the best way to get the root/base url of a web application in Spring MVC?
基本网址= http://www.example.com 或 http://www.example.com/VirtualDirectory
推荐答案
如果基本网址为 http://www.example.com ,请使用以下内容获取 www.example.com 部分,不带 http://:
If base url is "http://www.example.com", then use the following to get the "www.example.com" part, without the "http://":
来自控制器:
@RequestMapping(value = "/someURL", method = RequestMethod.GET)
public ModelAndView doSomething(HttpServletRequest request) throws IOException{
//Try this:
request.getLocalName();
// or this
request.getLocalAddr();
}
来自JSP:
在文档顶部声明以下内容:
Declare this on top of your document:
<c:set var="baseURL" value="${pageContext.request.localName}"/> //or ".localAddr"
然后,要使用它,请引用变量:
Then, to use it, reference the variable:
<a href="http://${baseURL}">Go Home</a>
这篇关于在Spring MVC中获取根/基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文