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
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.
I'm not sure I got your question right, but I have a similar setup for my project. I have 2 applications:
Web application deployed at http://localhost:8080/WebApplication
Mobile application deployed at http://localhost:8080/MobileApplication
I also have one url which is the same: /home
http://localhost:8080/WebApplication/home -> returns list of web-app features
http://localhost:8080/MobileApplication/home -> returns list of mobile-app features.
There are two parameters I deal with:
request.getContextPath() returns the name of the application -> WebApplication or MobileApplication
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With