SPRINGMVC's study (2) worth receiving and transmitting

Source: Internet
Author: User

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

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.