Several key points of SPRINGMVC use

Source: Internet
Author: User

1. Use the @RequestParam ("username") to correspond to the parameter name, this parameter must be passed in, otherwise it will be an error. If you don't add @requestparam, you can preach.

    @RequestMapping ("/index")    public string index (@RequestParam ("username ") string username, String password) {        System.out.println (username);        SYSTEM.OUT.PRINTLN (password);         return "Test/index";    }

2. To the page value, you can use map can also use model, usually use model

@RequestMapping ("/index2")     PublicString Index2 (string username, map<string, object>context)        {System.out.println (username); Context.put ("Username", username); return"Test/index"; } @RequestMapping ("/index3")     Publicstring Index3 (string username, model model) {//System.out.println ("ID:" + ID);System.out.println (username); Model.addattribute ("username", "username"); //only one parameter, the default key is the lowercase object nameModel.addattribute (Newarticle ()); return"Test/index"; }

Page:

Test ${username}${ Article.id}</body>

3. Method = Requestmethod.get represents GET access

    @RequestMapping (value = "/index4", method = requestmethod.get)    public  String index4 (string Username, model model) {        //  System.out.println ("ID:" + ID);         System.out.println (username);        Model.addattribute ("username", "username");         // only one parameter, the default key is the Lowercase object name        Model.addattribute (new  article ());         return "Test/index";    }

4. @PathVariable @RequestMapping value with the URL value can directly support the path of the multilevel directory, unlike ASP. NET MVC needs to set the route in Global.asax

    //WWW.URL.COM/TEST/P1 The address of the URL to read as a parameter@RequestMapping (value = "/{username}", method =requestmethod.get) Publicstring Show (@PathVariable string username) {System.out.println (username); return"Test/index"; }    //WWW.URL.COM/TEST/P1 The address of the URL to read as a parameter@RequestMapping (value = "/update/{id}", method =requestmethod.get) Publicstring Show2 (@PathVariable string id) {SYSTEM.OUT.PRINTLN ("Show2" +ID); return"Test/index"; } @RequestMapping (Value= "/update/detail/{id}", method =requestmethod.get) Publicstring show3 (@PathVariable string id) {SYSTEM.OUT.PRINTLN ("SHOW3" +ID); return"Test/index"; }

5. (1) can be done by Bean-validator.jar to SPRINGMVC server Check, check @validated menu menu, bindingresult br parameters must be written together

    @NotEmpty (message= "menu list cannot be empty")    public  String getmenuname () {        return menuname;    }

(2) Springmvc upload a file requires Commons-io-2.2.jar, Commons-fileupload-1.3.1.jar two jar package

The bean code needs to be added to the configuration file

<!--setup Multipartresolver to finish uploading files--
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!--limit file maximum size 5M--
<property name= "maxuploadsize" value= "5000000" ></property>
</bean>

@RequestMapping (value = "/menuadd", method =requestmethod.post) PublicString menuadd (@Validated menu menu, Bindingresult br, @RequestParam ("Attachs") multipartfile[] Attachs, HttpServletRequest req)throwsIOException {//follow validated write validation result classSystem.out.println (Menu.getmenuid () + "" +menu.getmenuname ()); if(Br.haserrors ()) {System.out.println ("Validation does not pass"); return"Redirect:/test/menuadd";//Client Jump        }         for(Multipartfile attach:attachs) {if(Attach.isempty ())Continue; System.out.println (Attach.getname ()+ "," + attach.getoriginalfilename () + "," +Attach.getcontenttype ()); String Path= Req.getsession (). Getservletcontext (). Getrealpath ("/web-inf/resources/upload");            SYSTEM.OUT.PRINTLN (path); File File=NewFile (path + "/" +attach.getoriginalfilename ());        Fileutils.copyinputstreamtofile (Attach.getinputstream (), file); }        return"Redirect:/test/index2";//Client Jump}

Page:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><% @taglib prefix=" SF "uri=" http://www.springframework.org/tags/form "% ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >MenuID:<input type= "text" name= "MenuID" ><br/>menuname:<input type= "text" name= "menuname" ><br><sf:errors path= "Menuname" ></sf:errors><br/>file1:<input type= "File" Name= "Attachs" ><br/>file2:<input type= "File" Name= "Attachs" ><br/>File3:<input type= "File" Name= "Attachs" ><br/><input type= "Submit" value= "Submit" ></sf:form></body >

6. Jump page can use return "REDIRECT:/TEST/INDEX2"; Client Jump

Several key points of SPRINGMVC use

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.