Overview
Spring MVC is made up of View-controller-model, where the data interaction between view and Controller becomes the core point of attention. In MVC, we pass the data in the view to the controller, and we can pass the corresponding parameters by post or get. The controller is bound to match the parameters passed by the foreground. The backend controller can also pass values to the foreground page.
View Value passing Controller
The common ways to bind values are as follows
- @RequestParam, bind order request data, can be the data in the URL, the form submitted data or uploaded files;
- @PathVariable, bind URL template variable value;
- @CookieValue, binding cookie data;
- @RequestHeader, bind the request header data;
- @ModelAttribute, bind data to model;
- @SessionAttributes, bind data to session;
- @RequestBody, used to deal with Content-type not application/x-www-form-urlencoded encoded content, such as Application/json, application/xml, etc.;
@RequestParam and @pathvariable Use an example controller as follows:
@RequestMapping (value="/city/{cname:\\w+}-{id:\\d+}", method=requestmethod.get) PublicModelandview chinacity (@PathVariable (value="ID") String ID, @PathVariable (value="CNAME") String Name,
@RequestParam ("Code") String usercode) {Modelandview MODV=NewModelandview (); Modv.addobject ("Cityid", ID); Modv.addobject ("CNAME", Name); Modv.addobject ("Code", Usercode); Modv.setviewname ("helloworld/chinacity"); returnMODV; }
Match parsing url:http://www.xxx.com/mvcdemo/hellowworld/city/beijing/23?code=988234
Examples of use of @CookieValue and @requestheader and @modelattribute are as follows
@RequestMapping ("/usercook") PublicModelandview Showusersessionmodel (@CookieValue ("Jsessionid") String Cookname, @RequestHeader ("user-agent") String Userage,
@ModelAttribute ("UserInfo") UserInfo user) {Modelandview RMV=NewModelandview (); Rmv.setviewname ("User/usercook"); Rmv.addobject ("cookmsg", cookname+"-"+userage); returnRMV; }
Parse: @CookieValue Parse Browser cookie value, @RequestHeader parse Browser request header head value, and @modelattribute parse form form binding model value, corresponding JSP is as follows
< from: Form action=""Method="Post"Modelattribute="UserInfo"> <p> < from: Label path="username"> User name: </ from:label>< from: Input path="username"/>< from: Errors Path="username"></ from:errors> </p> <p> < from: Label path="Userage"> User age: </ from:label>< from: Input path="Userage"/>< from: Errors Path="Userage"></ from:errors> </p> <P> <input type="Submit"Value="Submit"> </P> </ from:form>
@SessionAttributes store data in a session to keep data between requests, so that you can implement requirements such as step-through submission of a form. You need to bind the sessionattributes to the controller, and then the method inside the controller can adjust the session value
Package Justin.com.controllers;import Justin.com.models.userinfo;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.modelattribute;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.sessionattributes;import Org.springframework.web.bind.support.SessionStatus, @Controller @sessionattributes ("Sessionuser") @RequestMapping ("/usession") Public classSessusercontroller {@RequestMapping (Value=" /Home", method=requestmethod.get) PublicString Sesshome (@ModelAttribute ("Sessionuser") UserInfo user,sessionstatus status) {Status.setcomplete (); Read the session to assign a value to UserInfoif(user!=NULL) User.setuserage ( -); return "Usession/index"; }}
Writing usession/index.jsp pages
<body> ! <p> userage: ${sessionuser.userage} </p> <p> UserName: ${ Sessionuser.username} </p></body>
Request URL:
Http://www.xxx.com/MVCDEMO/USession/Home?userage=98&username=justin
@RequestBody call the appropriate messageconvert to convert the contents of the non-application/x-www-form-urlencoded request to the specified object.
It is usually used in combination with @responsebody. @ResponseBody in contrast to [email protected], he converts the specified object to the appropriate content (the request header is Accept:application/json to return the JSON data) and returns. Because spring defaults parsing JSON with Jackson, we're going to add Jackson-core-asl-1.9.13.jar and Jackson-mapper-asl-1.9.13.jar two packages to our project.
For example, use the controller as follows
@RequestMapping ("/detail") Public@ResponseBody UserInfo getuserdetail () {UserInfo Usermodel=NewUserInfo (); Usermodel.setusername ("Zhang"); Usermodel.setuserage ( -); System. out. println ("Update:"+usermodel.getusername ()); returnUsermodel; }
Visit Detail page, feedback JSON value, valid for SPRING-MVC3, other versions to be determined.
Controller Value Pass View
1. Use model value
@RequestMapping (value="/detail", method=requestmethod.get) public String showuserdetail (httpservletrequest request,model Model) { Model.addattribute (" userinfo"new userinfo ()); return " User/userhome " ; }
2. Using ViewModel to transmit value
Modelandview rm=New Modelandview (); Rm.setviewname ("user/usershow"); Rm.addobject ("userinfo", user);
3. Using HttpServletRequest
Public String Exception (HttpServletRequest request,exception ex) { Request.setattribute ("exceptionmessage", Ex.getmessage ()); return " Error " ; }
View Get Value:
${sessionuser.userage}
Spring MVC basic Knowledge collation? View interacts with controller data