1. Request narrowing
When accessing the equivalent of adding a layer of catalogs @RequestMapping comment on the class: project name / user / .......
1 // narrowing request 2 @RequestMapping ( "User" ) . 3 public class UserControoler {}
2. parameter binding
1 @ResponseBody 2 @RequestMapping("/findById.action") 3 public String findById(HttpServletRequest request,@RequestParam(name = "uid",required = true) Integer id){ // 必须要有参数uid 4 return "***"; 5 }
2.2 submission
1 @RequestMapping
(
path = "/queryAll.action",
method = {RequestMethod.POST,RequestMethod.GET}, 2 params = {"uid=1"} 3 )
3. controller returns the value the ModelandView method (most commonly used) String void
. 1 public ModelAndView test1 () {// the most common and convenient
2 ModelAndView ModelAndView = new new ModelAndView (); . 3 modelAndView.addObject ( "MSG", "Hello" ); . 4 modelAndView.setViewName ( "../ the index.jsp " ); . 5 . 6 return ModelAndView; . 7 }
. 1 public String test2 (the Model Model) { // Model data save request to a domain, the shared data forwarding, redirection, twice request, the data is not shared 2 model.addAttribute ( "msg", "hello " ); . 3 return " Forward: the index.jsp ../ " ; . 4 return " the redirect: the index.jsp ../ " . 5}
1 public void test3(HttpServletRequest request, HttpServletResponse response){ 2 request.setAttribute("msg","日月星辰"); 3 request.getRequestDispatcher("../index.jsp").forward(request,response); 4 }
5. File Upload
springmvc.xml - must be resolved fileUpload component id: multipartResolver
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
web.jsp
1 <form method="post" enctype="multipart/form-data" action="user/upload.action"> 2 <input type="file" name="file"> 3 <input type="submit" value="上传"> 4 </form>
Controller Class @RequestMapping ( "user") // narrowing request
. 1 @RequestMapping ( "/ upload.action" ) 2 @ResponseBody . 3 public String Upload (a MultipartFile File) throws IOException { . 4 // file name . 5 String OriginalFilename = file.getOriginalFilename (); . 6 String UUID = UUID.randomUUID (). . toString () Replace ( "-", "" ); // do not overlap with the 32-bit characters . 7 int (. "I = originalFilename.lastIndexOf" ); . 8 String suffix = originalFilename.substring (I); . 9 10 // the page file name attribute value 11 String name = file.getName(); 12 System.out.println(originalFilename); 13 System.out.println(name); 14 File file1 = new File("E:\\upload\\"+uuid+suffix); 15 file.transferTo(file1); 16 return "上传中"; 17 }