Today in my SPRINGMVC Project-book Management system, I want to log in to the users who will be logged in the session, the beginning is ready to use the Servlet API object in the controller, but has been unable to reference, do not know why, Do you want to import what specific package (if you have a friend who knows, you can trouble message to tell me, thank you), and then see the online said can be used @sessionattributes and @modelattribute annotations to achieve, the specific implementation of the following:
Usercontroller class:
@Controller
@SessionAttributes ("userid")//① attribute named "Users" in Modelmap
In the Session property list so that this property can be accessed across requests
public class Usercontroller {
@Autowired
Private UserService service;
@Autowired
Private Booksservice Booksservice;
Public UserService GetService () {
return service;
}
public void Setservice (UserService service) {
This.service = Service;
}
@RequestMapping (value= "register")
public string Register (string name,string password,map<string, object> params) {
Users users=new users ();
Users.setname (name);
Users.setpassword (password);
Users.setpoint (0);
Users.settype (0);
Users.setlevel (0);
Service.save (Users,params);
Params.put ("UserName", name);
if (Params.get ("ErrMsg")!=null) {
return "register";
}
return "Reg_success";
}
@RequestMapping (value= "Login")
Public String Login (the Users users,modelmap map){
Users users2 = service.findusers (users);
if (users2==null) {
Map.put ("errmsg", "Input error, please re-enter");
Return "";
}
Map.put ("Users", users2);
map.addattribute ("userid", Users2.getid ());
if (Users2.gettype () ==0) {
list<books> bookslist = Booksservice.findallbooks ();
Map.put ("Booksquerylist", bookslist);
Map.put ("Books", New Books ());
return "Visitor_first";
}else{
return "Manager_first";
}
}
}
In another bookinfocontroller to invoke the UserID:
@Controller
@SessionAttributes ("userid")
public class Bookinfocontroller {
@Autowired
Private Bookinfoservice service;
@Autowired
Private Booksservice Booksservice;
@Autowired
Private UserService UserService;
@Autowired
Private Recordsservice Recordsservice;
@RequestMapping (value = "Bookinfos")
Public String bookinfolist (@RequestParam ("bid") int bid, map<string, object> Map) {
list<bookinfo> Bookinfos = service.findbookinfobybid (BID);
Books Books = Booksservice.findbooksbyid (BID);
Map.put ("Bookinfolist", Bookinfos);
Map.put ("Books", books);
return "Bookinfolist";
}
@RequestMapping (value= "Lendbooks")
Public String Lendbooks (@RequestParam ("id") int ID,@ModelAttribute ("userid") int uid,map<string,object> Map) {
Service.updatebookinfo (ID);
System.out.println ("UserId:" +uid);
BookInfo info = Service.findbookinfobyid (ID);
Records records=new Records ();
Users users = Userservice.findusersbyid (UID);
Records.setusers (users);
Records.setinfo (info);
Records.setlendtime (New Date ());
Recordsservice.addrecords (Records);
Map.put ("MSG", "borrowing success");
return "Saveshow";
}
}
The above has been successfully tested, but before the success of a lot of detours:
1. At the beginning, I saved the persistent (ID value) Users object in @sessionattributes, but the ID value of the Users object obtained in @modelattribute is self-growing every time, and the other properties are unchanged. I don't know whether I built a table has a problem or a configuration file has a problem, find a half-day also did not find, have to know the friend also trouble message tell me next.
2. In the online inquiry saw a friend said @sessionattributes ("userid") in the "userid" must contain a capital letter, after my experiment, absolutely no need ah!
Try hard and try harder!
Storing data in the session domain using @sessionattributes and @modelattribute in SPRINGMVC