Spring MVC controller receives the request value and the controller jumps and passes the value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.