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
  • 17,258
  • 9
  • 62
  • 65
Pracede
  • 4,226
  • 16
  • 65
  • 110

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
  • 14,038
  • 12
  • 56
  • 85
Pulkit
  • 3,953
  • 6
  • 31
  • 55