将contextpath参数添加到@GetMapping

时间:2018-04-11 22:06:10

标签: java spring-boot thymeleaf

我正在尝试创建我的路由,而不依赖于application.properties中的server.contextPath

这是一个例子:

@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";

一切都很好,但有时候我不需要重定向。

背后的想法是按照我在此链接中的要求,按照流程在tomcat中部署为war文件: How to get context path in controller without set in application.properties

所以问题是:是否可以在@GetMapping中添加一些参数来添加contextPath

更新1

我不确定你在问什么。

假设我说我在同一个项目中创建了两个war项目,名为webapp1和webapp2,我在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 中的百万富翁页面时,找不到该页面(这是错误的),因为在 方法 @GetMapping 无法找到可能是webapp1或webapp2的contextPath。

我不想使用server.contextPath,因为在这种情况下,我认为你只能拥有一个名为server.contextPath的项目。

由于

2 个答案:

答案 0 :(得分:4)

Spring维护它的上下文路径,因此您不必担心这一点。 你的代码看起来很好。

你可以试试。

尝试从application.properties文件中完全删除server.contextPath行。停止服务器清理和构建,重新启动服务器并再次启动应用程序。

答案 1 :(得分:3)

I'm not sure I got your question right, but I have a similar setup for my project. I have 2 applications:

  1. Web application deployed at http://localhost:8080/WebApplication

  2. Mobile application deployed at http://localhost:8080/MobileApplication

I also have one url which is the same: /home

  1. http://localhost:8080/WebApplication/home -> returns list of web-app features

  2. http://localhost:8080/MobileApplication/home -> returns list of mobile-app features.

There are two parameters I deal with:

  1. request.getContextPath() returns the name of the application -> WebApplication or MobileApplication

  2. request.getServletPath() returns the path after the context. In my case, it returns "/home" for both the applications.

Also, if you need to pass parameters, why are you using param1 in the url? It should be something like this:
http://localhost:8080/webapp1/dashboard/admin/list?param1=100

http://localhost:8080/webapp2/dashboard/admin/list?param1=200

In which case, your code would be:

@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";
}

I also suggest you to look into using @RequestMapping instead of @GetMapping This is what I use for my project:

@RequestMapping(value = "/home", method = RequestMethod.GET)