1

I'm using Thymeleaf with Springboot2.1.2 and I have problem accessing session attributes in the templates.
Here's the code:

This is one of the controllers:

@GetMapping("/profile")
public String getProfile(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    String email = (String) session.getAttribute("userId");
    User user = userService.getProfile(email);
    session.setAttribute("user", user);
    return "user/profile";
}

And the corresponding view(html):

<body th:object="${session.user}">
    //some code using the user object here...
</body>

When I run the application, I got the exception:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'session' available as request attribute

I've also tried for #session and something else, they didn't work. However, in another controller, I can access the object by using Model:

@GetMapping("/register/user")
public String registerUser(Model model) {
    model.addAttribute("user", new User());
    return "user/register";
}

And the view is like:

<form th:object="${user}" method="post" action="#" th:action="@{/register/user}">
    //some code using the user object...
</form>

It's driving me crazy since all the tutorials I can find tell me I can access session attributes by ${session.something}, in reality, it doesn't work.
Could you help me?

2 Answers 2

0

You are saving the information in the session, this is not visible by thymeleaf. you need to create a model for your thymeleaf template and add the attribute (or the session) to the model and then return that.

@GetMapping("/profile")
public ModelAndView getProfile(HttpServletRequest request) {
    User user = userService.getProfile(email);
    ModelAndView model = new ModelAndView(NAME_OF_THYMELEAF_PROFILE_FILE);
    model.addObject("user",user);
    return model;
}

Beware that for the thymeleaf to see the template it needs to be in a default path (resources/templates) or you need to define where your templates are stored.

If you want to use session again the solution is similar.

@GetMapping("/profile")
public ModelAndView getProfile(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    User user = userService.getProfile(email);
    session.setAttribute("user", user);
    ModelAndView model = new 
    ModelAndView(NAME_OF_THYMELEAF_PROFILE_FILE);
    model.addObject("session",session);
    return model;
}

UPDATE use model and return String:

@GetMapping("/profile")
public String getProfile(HttpServletRequest request, Model model) {
    HttpSession session = request.getSession(false);
    String email = (String) session.getAttribute("userId");
    User user = userService.getProfile(email);
    session.setAttribute("user", user);
    model.addAttribute("session", session);
    return "user/profile";
}

I used ModelAndView, you can do the same with Model, just instead of addObject() you must use addAttribute().

6
  • yes, ModelAndView does work, but i wonder why the session is invisible to Thymeleaf? Here's a reply to another question i've found in the official forum:
    – QYC
    Feb 11, 2019 at 11:05
  • forum
    – QYC
    Feb 11, 2019 at 11:05
  • Is there any secret i've missed?
    – QYC
    Feb 11, 2019 at 11:06
  • and i kind of feel like returning a ModelAndView is not as elegant as returning a string
    – QYC
    Feb 11, 2019 at 11:10
  • Actually, I find it more elegant to return the model rather than the String. But on your second case you insert the user into the model, that is why it works. You need to import the information that you need into the model, the session is not connected directly with thymeleaf. Check out this tutorial.
    – Dimitris
    Feb 11, 2019 at 11:23
0

You should be using Spring-Security with thymeleaf extensions to accomplish what you want. Here is an example of what you want to do. If you follow the example you can access user information as follows:

<div sec:authentication="name"><!-- Display UserName Here --></div>

Please note that for spring-boot 2.1.X you should use the following dependency:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.