我正在尝试创建我的路由,而不依赖于server.contextPath中的application.properties
这就是一个例子:
@PreAuthorize("hasRole('ROLE_ADMIN')
@GetMapping("/dashboard/admin/list/param1/{param1}")
public String method(@PathVariable String param1, Model model, HttpServletRequest request) {
//Some stuff
String contextPath = request.getContextPath();
return contextPath + "/dashboard/admin/list";
}但正如预期的那样,由于contextPath添加了该视图,所以没有找到该视图。
如果我像这样重定向:
String contextPath = request.getContextPath();
String redirect = contextPath + "/dashboard/admin/list";
return "redirect:/dashboard/admin/directorio/list";一切都很好,但有时我不需要重定向。
后面的想法是按照我在这个链接:How to get context path in controller without set in application.properties中要求的在tomcat中部署为war文件的过程。
所以问题是:是否可以在@GetMapping中添加一些param来添加contextPath
更新1
我不知道你在问什么。
假设我从一个名为webapp1和webapp2的项目中创建了两个war项目,并部署在我的tomcat服务器中。
我可以像这样访问这两个项目:
http://localhost:8080/webapp1/dashboard/admin/list/param1/100
http://localhost:8080/webapp2/dashboard/admin/list/param1/200
但是,当我返回位于src/main/resources/templates/dashboard/admin/list.html中的thymeleaf页面时,页面就找不到了(这就是错误),因为在@GetMapping方法中找不到contextPath,后者可以是webapp1或webapp2。
我不想使用server.contextPath,因为在这种情况下,我认为您可以只有一个项目的名称为server.contextPath。
谢谢
发布于 2018-04-16 08:19:23
Spring维护它的上下文路径,所以您不必担心这一点。你的代码看起来很好。
你能尝试的。
尝试从server.contextPath文件中完全删除application.properties行。停止服务器清理和构建,重新启动服务器并再次启动应用程序。
发布于 2018-04-20 21:18:50
我不确定我是否正确地回答了你的问题,但我的项目也有类似的设置。我有两份申请:
我也有一个相同的url : /home
我处理两个参数:
另外,如果需要传递参数,为什么要在url中使用param1?应该是这样的:
http://localhost:8080/webapp1/dashboard/admin/list?param1=100
http://localhost:8080/webapp2/dashboard/admin/list?param1=200
在这种情况下,您的代码将是:
@GetMapping("/dashboard/admin/list")
public String method(@PathVariable String param1, Model model, HttpServletRequest request) {
//Some stuff
String localParam1 = param1;
//You can also use the following line. In which case, you can get rid of the @PathVariable in your method declaration.
String localParam1 = request.getParameter("param1");
String contextPath = request.getContextPath();
return contextPath + "/dashboard/admin/list";
}我还建议您考虑使用@RequestMapping而不是@GetMapping --这是我的项目所用的:
@RequestMapping(value = "/home", method = RequestMethod.GET)https://stackoverflow.com/questions/49785084
复制相似问题