0

I'm trying to do my application that is dedicated for managing patients data base by doctors and have some problems with keeping information about object that is once send by post request method. I want them to be remembered in the URL. I tried to do something with @SessionAttributes but I don't think i do understand it well.

Here's my controller:

@Controller
@SessionAttributes("loggedInPersonelID")
@RequestMapping
public class PatientManagerController {

@Autowired
private PatientService patientService;

@Autowired
private PersonelService personelService;


private Personel getLoggedInPersonel(String personelID) {
    return personelService.getPersonel(personelID);
}

@GetMapping
public ModelAndView getLoginView() {
    ModelAndView mav = new ModelAndView("login-view");
    mav.addObject("personel", new Personel());
    return mav;
}

Method post passes logged in user to next URL /user={id} (used RedirectAttributes)

@PostMapping
    public String loginUser(@ModelAttribute("personel") Personel personel,
                            RedirectAttributes redirectAttrs,
                            Model model) {

        Personel loggedInPersonel = getLoggedInPersonel(personel.getPersonelID());
        model.addAttribute("loggedInPersonelID", loggedInPersonel.getPersonelID());

        if (loggedInPersonel != null) {
            if (loggedInPersonel.getPassword().equals(personel.getPassword())) {

                redirectAttrs.addAttribute("id", loggedInPersonel.getPersonelID());
                return "redirect:/user={id}";
            } else {
                model.addAttribute("errorMessage", "Invalid credentials!");
                return "login-view";
            }
        } else {
            model.addAttribute("errorMessage", "User with given ID does not exist");
            return "login-view";
    }
}

Here's my get method that catches the view for logged in user. URL works here since the model was passed in previous post method. I've got something like /user=john-smith-123

@GetMapping("/user={id}")
public ModelAndView getUserMainView(@PathVariable("id") String personelID) {
    ModelAndView mav = new ModelAndView("personel-main-view");
    Personel loggedInPersonel = getLoggedInPersonel(personelID);

    mav.addObject("personelOccupation", loggedInPersonel.getOccupation());
    mav.addObject("personelName", loggedInPersonel.getName());
    mav.addObject("personelSurname", loggedInPersonel.getSurname());
    return mav;
}

However the next page doesn't remember the user's id anymore. I thought that passing it to the model's attribute with the same name as determined in @SessionAttributes("loggedInPersonelID") the information will be remembered.

@GetMapping("/user={id}/patients")
public ModelAndView getPatientsView(@PathVariable("id") String personelID) {
    ModelAndView mav = new ModelAndView("patients-view");
    Personel loggedInPersonel = getLoggedInPersonel(personelID);
    mav.addObject("loggedInPersonelID", loggedInPersonel.getPersonelID());
    mav.addObject("list", patientService.getPersonelsList(loggedInPersonel));
    return mav;
}

The outcome in the URL: user=$%7BloggedInPersonelID%7D/patients and error There was an unexpected error (type=Internal Server Error, status=500). No message available

Here's the link in a personel-main-view view that should move me to desired page

<a th:href="@{/user=${loggedInPersonelID}/patients}">My patients</a>

So how do I do that? Sorry for the messy code. Is this matter more complicated than it looks like? Is there something deeper I am missing?

PS. I am working with thymeleaf

bartuomiei
  • 45
  • 1
  • 7

1 Answers1

0

try this:

<a th:href="@{/user=__${loggedInPersonelID}__/patients}">My patients</a>

this works as shown here

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • doesn't work and frankly speaking I don't why should it. I'm having problems with saving object information in jumping between pages – bartuomiei Nov 10 '17 at 19:10
  • It should due to preprocess expression `__.` It should preprocess expression and pass eg user=1234 as string to `@` expression, – Antoniossss Nov 10 '17 at 21:59