1

I'm a newbie in terms of Spring and Springboot and some aspects are still magic for me. Is it possible to pass automatically parameters to URL from a form on previous URL? The thing is:

I fill the form with username and password at localhost:8080/login-page. E.g. johny123 / pass123.

Then i want it move to the URL adress localhost:8080/user=johny213. I've been looking for answers, and I don't think @RequestParam solves the problem.

@PostMapping("/user={userId}")
public ModelAndView loginUser(@RequestParam ("userId") String userID, @ModelAttribute ("user") User user){

creating MAV, code code code, returning MAV
}

The thing is, I don't want to type parameters in URL, I want them to be automatically passed from a form. I haven't shown more code, because I just need a general idea and hints how to implement this.

Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36
bartuomiei
  • 45
  • 1
  • 7

1 Answers1

1

To be honest im not 100% sure what you are asking for. Is hit "How to submit form data?" or is it "How to pass redirect attributes?"

If its the second, I think the Object you are looking for is called RedirectAttributes. You can inject this to your controller method and set your redirect parameters.

For example like this:

@PostMapping("/login")
public ModelAndView loginUser(@RequestParam ("userId") String userID, @ModelAttribute ("user") User user, RedirectAttributes attr){
  ...
  attr.addFlashAttribute("user",user);
  attr.addFlashAttribute("userId", userID);
  return modelAndView;

}

BUT the code you posted looks like you want to determine the current user. The user is stored in the session and you don't need to pass it everytime. Someone demonstrated in this answer on stackoverflow how to get the current user.

Yannic Bürgmann
  • 6,301
  • 5
  • 43
  • 77
  • Detmining current user seems complicated as people suggest doing so in the given link. Somehow I manage getting the logged in User from my collection. I want to have seperate web pages for each of them. I have changed my method a little bit, but the RedirectAttributes doesn't work. I believe it the thing I am looking for, however something is wrong. – bartuomiei Nov 03 '17 at 09:18
  • Please dont post code in comments. Update your question with what you have tried and tell us what the result is and how it differs from what you want to achieve – Yannic Bürgmann Nov 03 '17 at 09:21