Y
Y
Yuri2017-01-21 11:59:14
Java
Yuri, 2017-01-21 11:59:14

How to generate user profile link via MVC?

Actually, I myself do not quite understand how to correctly formulate a question, so I will write as I understand it.
I have a controller that redirects the logged in user to the url: localhost:8080/profile/username

@RequestMapping(value = {"/profile","/profile/**"}, method = RequestMethod.GET)
    public String profile(Model model){

        if(authorizedUser.getUserByUsername() != null) {
            User user = authorizedUser.getUserByUsername();
            model.addAttribute("user",user);
        }
        return "profile";
    }


How can I make a controller to display not only logged in users, but anyone from the database?

That is, you follow the link: localhost:8080/profile/username3 and get his profile from the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kuznetsov, 2017-01-21
@MoonMay

Well, for example like this:

@RequestMapping(value = {"/url/to/profileAction/{id}"}, method = RequestMethod.GET)
public ModelAndView profileAction(@PathVariable("id") Long id) {
  ModelAndView modelAndView = new ModelAndView();
  modelAndView.setViewName("profile");
  
  UserDTO data = getUserDTOsService().getById(id);

  if (data != null) {
    modelAndView.getModel().put("data", data);
    return modelAndView;
  }
  
  return "redirect:/";/*ну или бросить исключение, что нет пользователя*/
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question