0

I have a login page where the user can log into myb application. When the user click on the button "log in", the request is processed by the following controller :

@Controller
@SessionAttributes("user-entity")
public class LoginController extends BaseController {

    @RequestMapping(value="/login", method = RequestMethod.GET)    
    public ModelAndView login(WebRequest webRequest, @ModelAttribute("user") User user) {
        return  new ModelAndView(VIEW_LOGIN_NAME, "user-entity", user);
    }

In the above code, I have added the user-entity ( user ) in the session. But I am not able to retrieve the user-entity in another controller :

@Controller
public class ProfileController extends BaseController{  
@RequestMapping(value="/saveprofil", method = RequestMethod.POST)
public ModelAndView saveProfile(@ModelAttribute Profil profil,HttpSession session){
    User user = (User) session.getAttribute("user-entity");

    user.setProfil(profil);
    userServices.createUser(user); 
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("saveProfilSuccess");
    return modelAndView;
} 

The user object is not null but all attributes are null. What's wrong?

Raidri
  • 16,630
  • 9
  • 58
  • 64
Pracede
  • 4,100
  • 14
  • 62
  • 105

1 Answers1

0

@praacede: try this code u will get the persisted value of user

public ModelAndView login(WebRequest webRequest, @ModelAttribute("user") User user,HttpSession session{

    session.setAttribute("User-entity",user);
    return new ModelAndView(VIEW_LOGIN_NAME, "user-entity", user); 
}
gipinani
  • 13,294
  • 11
  • 53
  • 82
Pulkit
  • 3,743
  • 5
  • 28
  • 54