Spring receives request parameters:
1, using HttpServletRequest to get Java code @RequestMapping ("/login.do") public String Login (HttpServletRequest request) {string name = Request.getpa Rameter ("name") String pass = Request.getparameter ("Pass")}
2,spring automatically injects form parameters into method parameters, and the Name property of the form remains the same. As with Struts2 Java code @RequestMapping ("/login") public String Login (HttpServletRequest request, String name, @RequestParam ("pass") string password)//The form attribute is pass, and the variable password receives {log.info (name); Log.info (password)}
3, automatically inject bean properties
Java code <form action= "Login" > User name: <input name= "name"/> Password: <input name= "pass"/> <input type= "su Bmit "value=" "Login" > </form>//Package User-Class public class user{private String name; Private String Pass; }
Java code @RequestMapping ("/login") public String login (user user) {Log.info (User.getname ()); Log.info (User.getpass ()); }
to pass a value to a page:
when the controller component is processed, the value is passed to the JSP page,
1, use HttpServletRequest and Session and then setattribute (), just like in a servlet
2, using the Modelandview object
3, using the Modelmap object
4, using @modelattribute annotations
The model data will use HttpServletRequest's attribute to pass the value to Java code @RequestMapping ("/login") public in success.jsp Modelandview Login (String name,string pass) {User user = Userservice.login (name,pwd); map<string,object> data = new hashmap<string,object> (); Data.put ("user", user); return new Modelandview ("Success", data); }
examples of using the Modelmap parameter object:
The modelmap data is used to transfer values from HttpServletRequest attribute to Java code in success.jsp @RequestMapping ("/login.do") public String Login (String name,string Pass, Modelmap model) {User user = Userservice.login (name,pwd); Model.addattribute ("user", user); Model.put ("name", name); Return "Success"; }
using the @modelattribute sample
used on the parameter part of the controller method or the Bean property method
@ModelAttribute data will be attribute to success.jsp using HttpServletRequest.
What is the difference between the Modelmap object's Addattribute,put two methods is that AddAttribute is not allowed to add a null value key,put is allowed
Java code @RequestMapping ("/login.do") public String Login (@ModelAttribute ("user") user user) {//todo return " Success "; } @ModelAttribute ("name") public String GetName () {return name; }
Session Storage:
can take advantage of Httpservletreequest's GetSession () method