How can I get 3 different path url in @RequestMapping SpringBoot + Thymeleaf

1.2k views Asked by At

I have question about @RequestMapping in Spring Boot.

I want to do login() method where depends than user role he will be redirect to different path.

For example I have 3 roles for user:

  • admin
  • student
  • teacher

In thymeleaf I using form th:action like:

<form th:action="@{/login}" th:object="${userR}" method="post">

And I make method:

@RequestMapping(value = "/login", method=RequestMethod.POST)
public String login(Model model, @ModelAttribute("userR") User user) {

And for those 3 roles I always logging to address/login but I want login to

  • address/admin
  • address/student
  • address/teacher

How can I set this url path for each role?

For instance just make if and "return address/student" redirect me to correct page but the url still is "address/login".

For each role will be different @Controller.

1

There are 1 answers

1
S R Chaitanya On BEST ANSWER

In Spring when you return the URL as "address/student" then it will be considered as a forward request and so will not make a round trip to the client and thus will not change the URL on client side.

If you want to change the URL, the request has to be a redirect request in which case the request will make a round trip to the client. This is the case whether it is Servlet framework or Spring framework.

And the right way to do that in Spring is to prefix the URL with text "redirect:"
For example: return "redirect:xyzPage"