Receive request parameter one, use HttpServletRequest
@RequestMapping ("/login") public String Login ( HttpServletRequest request) { = Request.getparameter ("name"); = Request.getparameter ("pwd"); return " Success " ; }
Second, automatic parameter matching-----
Springmvc automatically injects form parameters into method parameters, as long as the form's Name property is consistent
Front-End Request form
<form action= "${pagecontext.request.contextpath}/getparam" method= "POST" > User name: <input type= "text" Name= " Name "> Password: <input type=" text "name=" pwd "><input type=" Submit "value=" Submit "></form>
Controller code
@RequestMapping (value= "/getparam", method=requestmethod.post) public String getParam1 (String name,string pwd) { System.out.println ("User name" +name); SYSTEM.OUT.PRINTLN ("password" +pwd); return "Success";}
Third, the Bean object automatic Boxing
Package com.neuedu.bean;/* * Project name: SpringMVC-02 * @author: wzc* @date created: August 22, 2017 morning 9:23:33* @Description: Define Student class * @ Parameter * */public class Student {private string name;private string email;private int sex;private Address address;public Student () {super ();} Public Student (string name, string email, int sex) {super (); this.name = Name;this.email = Email;this.sex = sex;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getemail () {return email;} public void Setemail (String email) {this.email = email;} public int Getsex () {return sex;} public void setsex (int sex) {this.sex = sex;} Public Address getaddress () {return address;} public void setaddress (address address) {this.address = address;} @Overridepublic String toString () {return "Student [name=" + name + ", email=" + email + ", sex=" + Sex + ", address=" + A Ddress + "]";}}
Controller Code Snippet
@RequestMapping ("/getstudent") public String getstudent (Student Student) {string name = Student.getname (); String email = student.getemail (); System.out.println ("Name:" +name); System.out.println ("email:" +email); String Studnetin = student.tostring (); System.out.println (Studnetin); return "Success";}
Four
@RequestParam Get parameters
<a href= "${pagecontext.request.contextpath}/testurl?id=5" > Walk one, Test url</a><br>
@RequestMapping (value= "/testurl", method=requestmethod.get) public String Testurl (@RequestParam (value= "id") Integer ID) {// Service.getorderbyid (ID) System.out.println ("---------" +id); return "Success";}
Three default properties:
Value: This field should match the value of the Name property of the request parameter!
Required: Boolean, the default is true, when specified as false, indicating that this parameter is not necessary, can not take!
DefaultValue: When we do not pass the value, the default is to use the value of DefaultValue, pass the parameter, use the parameter value we pass! Five
@PathVariableGet placeholders for bindings in URLs
<a href= "${PAGECONTEXT.REQUEST.CONTEXTPATH}/ORDER/1" > Test get</a><br>
@RequestMapping (value= "/order/{id}") Public String getorderbyId1 (@PathVariable (value= "id") Integer ID) {// Service.getorderbyid (ID) System.out.println ("---------" +id); return SUCCESS;}
Vi. @RequestHeader: Get request header information
Value: This field should match the value of the Name property of the request parameter!
Required: Boolean, the default is true, when specified as false, indicating that this parameter is not necessary, can not take!
DefaultValue: When we do not pass the value, the default is to use the value of DefaultValue, pass the parameter, use the parameter value we pass!
Vii. MVC's Handler method can accept parameters of the SERVLETAPI type
HttpServletRequest
HttpServletResponse
HttpSession
Public String Login (httpservletrequest request,httpservletresponse response, HttpSession session) {}
To page one, use
SetAttribute ()
using HttpServletRequest and Session
such as: request. SetAttribute ("User", New user ("Zhang San", 1));
Two, Spring MVC provides several ways to output model data
Modelandview, Map, Model,modelmap
–modelandview: The method body can add model data through the object when the processing method returns a value of type Modelandview
–map and Model, Modelmap: entry for Org.springframework.ui.Model, Org.springframework.ui. Modelmap or JAVA.UTI.MAP,
When the processing method returns, the data in the MAP is automatically added to the model.
@RequestMapping ("/testmoudle") PublicString Testmoudle (map<string,object>map) {Map.put ("User","Zhang San"); return "Success"; } @RequestMapping ("/testmoudle1") PublicModelandview TestMoudle1 () {Modelandview Mandview=NewModelandview (); Mandview.addobject ("User","John Doe"); Mandview.setviewname ("Success"); returnMandview; } @RequestMapping ("/testmoudle2") PublicString testmoudle (Modelmap modelmap) {Modelmap.addattribute ("User","Harry"); return "Success"; } @RequestMapping ("/testmoudle3") PublicString TestMoudle2 (model model) {Model.addattribute ("User","Zhao Qi"); return "Success"; }
Whether our return value is a string type or a Modelandview type, the SPRINGMVC framework executes the target handler method and resolves the return value to Modelaview;
The data we put into the map or model and Modelmap will be put into the Modelandview object and used as model!
In front-end pages, you can remove parameters by scope
Iii. SPRINGMVC using JSON data
1. Add 3 jar packages to JSON
Jackson-annotations-2.1.5.jar
Jackson-core-2.1.5.jar
Jackson-databind-2.1.5.jar
2. Write the target method so that it returns the JSON corresponding object or collection
3. Add @ResponseBody annotations on the method
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
@RequestMapping (value= "/testjson", Method=requestmethod.post) @ResponseBodypublic list<student> Testjson () {list<student> list=new arraylist<> () list.add (New Student ("Zhang San", 0)); List.add (New Student ("John Doe ", 1)); List.add (New Student (" Harry ", 0)); return list;}
Springmvc notes (eight) parameter passing in SPRINGMVC