在百里香中,我们有:
<a th:href="@{/somepath}">Link</a>
它成为了:
<a href="http://hostname:port/somepath">Link</a>
我想获取完整的 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";
}
编辑 这个问题可以解释为:
public static String APPLICATION_BASE_URL = "http://hostname:port";
function someMethod(String method){
    return APPLICATION_BASE_URL + method;
}
我认为这APPLICATION_BASE_URL很丑陋,因为我可以在任何地方部署。我想知道 spring boot 甚至 java 中有一个漂亮的函数来获取我的应用程序的基本 URL。
这样做的方法是使用HttpServletRequest
@GetMapping(path="/")
public String index(HttpServletRequest httpServletRequest) {
    String link = httpServletRequest.getRequestURL();
    return "index";
}
| 归档时间: | 
 | 
| 查看次数: | 11430 次 | 
| 最近记录: |