Home > Back-end >  Spring session attribute lost on page calling itself
Spring session attribute lost on page calling itself

Time:05-22

So, I've got a very simple 2 controller Spring MVC app. First controller logs user in and puts it on session like this

model.addAttribute( "user", userDto );

Second controller displays project based on projectId, but expects user to be in session.

@Controller
@SessionAttributes( "user" )
@RequestMapping( value = "/project" )
public class ProjectController {
...
    @GetMapping( "/{projectId}" )
    public String get( @PathVariable( "projectId" ) String projectId, @SessionAttribute( "user" ) UserDto userDto, Model model ) {
...

First call to this controller works just fine. However, if user changes project on UI ( through select onchange="'location = /project/<some-id-from-select>" ) I'm getting error message from Spring:

[org.springframework.web.bind.ServletRequestBindingException: Missing session attribute 'user' of type UserDto]

How is it possible that session attribute gets lost after one request? No filters, security or session/request specific configurations are defined in the project.

CodePudding user response:

Well, actually it got fixed much easier: I've forgotten to add @SessionAttributes( "user" ) in first controller, where model.addAttribute( "user", userDto ); gets called. After adding it, everything works like a charm.

  • Related