6

I'm trying to create my routes without depending on the server.contextPath in application.properties

This is an example:

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

but as expected the view is not found because of the contextPath added.

If I make a redirect like this:

String contextPath = request.getContextPath();
String redirect = contextPath + "/dashboard/admin/list";
return "redirect:/dashboard/admin/directorio/list";

everything works great, but sometime I don't need to redirect.

The idea behind, is to follow the process to deploy as a war file in tomcat as I asked in this link: How to get context path in controller without set in application.properties

So the question is: Is possible to add some param in @GetMapping to add the contextPath

UPDATE 1

I'm not sure what you are asking.

Lets say I create two war projects from the same project called webapp1 and webapp2 and I deploy in my tomcat server.

I can acces both projects like this:

http://localhost:8080/webapp1/dashboard/admin/list/param1/100

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

but when I make the return to my thymeleaf page located in src/main/resources/templates/dashboard/admin/list.html the page is not found (that's the error), because in the method the @GetMapping cant find the contextPath which could be webapp1 or webapp2.

I don't want to use server.contextPath because in that case I think you can have just one project with the name of the server.contextPath.

Thanks

davisoski
  • 727
  • 2
  • 14
  • 41
  • What is the contextPath coming as when it doesn't work? Can you post the exception too? – Tarun Lalwani Apr 14 '18 at 05:58
  • Added some info. Hope you can help me... – davisoski Apr 14 '18 at 17:59
  • Why are those separate web apps? Do they represent the same domain resources? Because then if expect a load balancer or suchlike handling the different paths. – daniu Apr 14 '18 at 18:11
  • Hi. When you create a war file the name is like webapp1-0.0.1-SNAPSHOT. So you should change the name to deploy it in your server. Which name will you use???. I want the name not depend of the name used in server.contextPath in application.properties. The two names was ilustrative... – davisoski Apr 14 '18 at 21:26
  • Okay, but you do want to link back into the same web app, right? Why don't you just use a relative path? "../.." should do the trick, no? – daniu Apr 15 '18 at 07:34
  • ok, and how will you modify the code??? to get that relative path. You modify in @GetMapping or in the return .... ?? – davisoski Apr 15 '18 at 18:05
  • Does this help? https://stackoverflow.com/questions/12236590/retrieving-the-servlet-context-path-from-a-spring-web-application – Tarun Lalwani Apr 16 '18 at 05:59
  • You do not have to use contextPath, returning views should work out of the box no matter how many same webapps you have in tomcat. – Vikram Palakurthi Apr 19 '18 at 18:16

2 Answers2

5

Spring maintains it context path so you do not have to worry about that. Your code looks fine.

what you can try.

Try to remove server.contextPath line completely from application.properties file. stop server clean and build, restart the server and launch application again.

cool cool
  • 67
  • 10
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)
Raghuram Kasyap
  • 305
  • 2
  • 9