在 Spring MVC 中获取根/基本 URL [英] Get Root/Base Url In Spring MVC

查看:43
本文介绍了在 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.comhttp://www.example.com/VirtualDirectory

推荐答案

If base url is "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:

在文档顶部声明:

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆