【问题标题】:Add contextpath param to @GetMapping将 contextpath 参数添加到 @GetMapping
【发布时间】:2018-09-21 22:18:56
【问题描述】:

我正在尝试创建我的路由,而不依赖于 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

我不确定你在问什么。

假设我从名为 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 中的 thymeleaf 页面时,找不到该页面(这是错误),因为在 @GetMapping 方法找不到可能是 webapp1 或 webapp2 的 contextPath。

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

谢谢

【问题讨论】:

  • 什么是 contextPath 当它不起作用时?你也可以发布异常吗?
  • 添加了一些信息。希望你能帮助我...
  • 为什么是那些独立的网络应用程序?它们代表相同的域资源吗?因为如果期望负载均衡器或类似的东西处理不同的路径。
  • 嗨。当你创建一个 war 文件时,名称类似于 webapp1-0.0.1-SNAPSHOT。因此,您应该更改名称以将其部署在您的服务器中。你会用哪个名字???我希望名称不依赖于 application.properties 中 server.contextPath 中使用的名称。这两个名字很形象……
  • 好的,但是您确实想链接回同一个网络应用程序,对吧?为什么不直接使用相对路径? “../..”应该可以解决问题,不是吗?

标签: java spring-boot thymeleaf


【解决方案1】:

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

你可以尝试什么。

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

【讨论】:

  • 嗨。我在 application.properties 中没有 server.contextPath。
【解决方案2】:

我不确定我的问题是否正确,但我的项目有类似的设置。 我有 2 个应用程序:

  1. Web 应用程序部署在 http://localhost:8080/WebApplication

  2. 移动应用部署在http://localhost:8080/MobileApplication

我也有一个相同的网址:/home

  1. http://localhost:8080/WebApplication/home -> 返回网络应用功能列表

  2. http://localhost:8080/MobileApplication/home -> 返回移动应用功能列表。

我处理的参数有两个:

  1. request.getContextPath() 返回应用程序的名称 -> WebApplication 或 MobileApplication

  2. request.getServletPath() 返回上下文之后的路径。在我的例子中,它为两个应用程序返回“/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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2015-04-24
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多