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