Answer the question
In order to leave comments, you need to log in
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";
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question