In Springmvc
@Controller to identify a controller
@RequestMapping to identify the request path, which can be written on the class name or on the method name. Write in the class, indicating that all methods are under this path.
Package Com.sun.action;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.RequestMapping, @Controller @requestmapping ("/view") Public classViewpagecontroller {@RequestMapping ("/index") PublicString Index () {return "Index"; } @RequestMapping ("/index2") PublicString Index2 () {return "Index2"; }}
Pass values to the way the page is displayed
One is model one is Modelandview
@RequestMapping ("/hello") public String Hello (model model) { // arguments are passed to the page display Model.addattribute ("name""pagename ====== Hello" ); return " Hello " ; }
The return content here is the name of the JSP.
//returns the page with the return value@RequestMapping (value ="/querylistmv.do") PublicModelandview QUERYLISTMV (httpservletrequest request, httpservletresponse response) {Modelandview m V=NewModelandview (); Mv.setviewname ("/newuser");//return page nameMv.addobject ("Data",NewUser ());//return to map object returnMV; }
The content inside the viewname is the name of the JSP page.
Receiving of parameters
The first is the value of receiving a form or URL directly from the request.
@RequestMapping (value ="/querylistmv2.do", method=requestmethod.get) PublicModelandview queryListMV2 (httpservletrequest request, httpservletresponse response) {Strin G Name= Request.getparameter ("name"); Integer Age= Integer.valueof (Request.getparameter (" Age")); User User=NewUser (); User.setname (name); User.setage (age); Modelandview MV=NewModelandview (); Mv.setviewname ("/newuser");//return page nameMv.addobject ("Data", user);//return to map object returnMV; }
The second gets the specified arguments from the path
/* * @PathVariable Specify the parameters above path */@RequestMapping ("/pathview/{age}/{years}/{month}") Public String Pathview (model , @PathVariable (value= "age") string age, @PathVariable (value= "Years") string years, @PathVariable (value= "Month") String month) {Model.addattribute ("age", age), Model.addattribute ("Years", years); Model.addattribute ("Month", month) ; Model.addattribute ("Name", "page name ====== pathview"); return "Pathview";}
The Third kind: Receive with Requestparam
/** @RequestParam The parameters used to receive hyperlinks, you can set the default values*/@RequestMapping ("/userinfo") PublicString UserInfo (model model, @RequestParam (value="name", defaultvalue=Adminuser) String name) {if("Admin". Equals (name)) { //arguments are passed to the page displayModel.addattribute ("name","page name ======"+name); }Else{Model.addattribute ("name","page name ======"+name); } return "UserInfo"; }
Fourth, receive form objects with @ModelAttribute
/** * Pass object, through object, accept object value of form form*/@RequestMapping (Value="/adduser", method=requestmethod.post) PublicString AddUser (model model, @ModelAttribute ("Springweb") (user user) {Model.addattribute ("name", User.getname ()); Model.addattribute (" Age", User.getage ()); Model.addattribute ("ID", User.getid ()); System. out. println ("12231313"); return "AddUser"; }
SPRINGMVC's study (2) worth receiving and transmitting