我正在使用Spring MVC编写Java Web应用程序.我有一个后台进程通过数据库查找必须通过电子邮件发送给我的用户的通知.这些电子邮件消息需要包含应用程序的超链接.这似乎是一个相当常见的网络应用程序模式,但我遇到了麻烦.
如何使用服务器名称和上下文路径派生应用程序的完全限定URL?我无法访问HttpServletRequest中的任何方法,因为我将其作为后台进程运行,而不是响应Web请求.我能做的最好的就是访问ServletContext.
目前我将基本URL放入配置文件并在启动时读取它,但是此应用程序将获得许可并部署到客户的应用程序服务器,如果可能,我希望他们不必手动配置它.
在Spring MVC中获取Web应用程序的根/基本URL的最佳方法是什么?
Base Url = http://www.example.com或http://www.example.com/VirtualDirectory
在百里香中,我们有:
<a th:href="@{/somepath}">Link</a>
Run Code Online (Sandbox Code Playgroud)
它成为了:
<a href="http://hostname:port/somepath">Link</a>
Run Code Online (Sandbox Code Playgroud)
我想获取完整的 URL 给定路径,就像控制器中的路径一样,例如:
@GetMapping(path="/")
public String index(SomeInjectedClass cls) {
String link = cls.someMethod('/somepath');
// expected, link = http://hostname:port/somepath
return "index";
}
@GetMapping(path="/home")
public String home(SomeInjectedClass cls) {
String link = cls.someMethod('/somepath');
// expected, link = http://hostname:port/somepath
return "home";
}
Run Code Online (Sandbox Code Playgroud)
编辑 这个问题可以解释为:
public static String APPLICATION_BASE_URL = "http://hostname:port";
function someMethod(String method){
return APPLICATION_BASE_URL + method;
}
Run Code Online (Sandbox Code Playgroud)
我认为这APPLICATION_BASE_URL很丑陋,因为我可以在任何地方部署。我想知道 spring boot 甚至 java 中有一个漂亮的函数来获取我的应用程序的基本 URL。
我使用Icefaces和JSF,我遇到了这个问题:我有以下网址:
http://myMappedServer/myApp/followingThings
我想在我的xHtml页面中获取值http://myMappedServer/myApp
如何在不使用托管bean的情况下实现此目的?