@Controller @RequestMapping("/router") @SessionAttributes(value = { "username" }) public class RouterController { @RequestMapping(value = "/index", params = { "username", "age!=18" }) public String index() { return "index"; } // 通配符的使用 @RequestMapping(value = "/testantpath/*/abc") public String testantpath() { return "index"; } // pathVariable @RequestMapping(value = "/testpathvariable/{userId}") public String testpathvariable( @PathVariable(value = "userId") Integer userId) { System.out.println(userId); return "index"; } // RESTFUL @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET) public String testgetmethod(@PathVariable(value = "userId") Integer userId) { System.out.println("GET:" + userId); return "index"; } @RequestMapping(value = "/user/{userId}", method = RequestMethod.POST) public String testPostmethod(@PathVariable(value = "userId") Integer userId) { System.out.println("POST:" + userId); return "index"; } @RequestMapping(value = "/user/{userId}", method = RequestMethod.PUT) public String testputmethod(@PathVariable(value = "userId") Integer userId) { System.out.println("PUT:" + userId); return "index"; } @RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE) public String testdeletemethod( @PathVariable(value = "userId") Integer userId) { System.out.println("DELETE:" + userId); return "index"; } // @RequestParam @RequestMapping(value = "/tesrequestparam") public String tesrequestparam( @RequestParam(value = "username", required = false) String username) { System.out.println(username); return "index"; } // @RequestHeader @RequestMapping(value = "/testrequestheader") public String testrequestheader( @RequestHeader(value = "Accept-Language") String al) { System.out.println(al); return "index"; } // @CookieValue @RequestMapping(value = "/testcookievalue") public String testcookievalue( @CookieValue(value = "JSESSIONID") String cookie) { System.out.println(cookie); return "index"; } /* * pojo的 获取 */ @RequestMapping(value = "/testuser") public String testpojo(TUser user) { System.out.println(user); // 数据库的操作 return "index"; } /* * ModelAndView */ @RequestMapping(value = "/testmodelandview") public ModelAndView testmodelandview() { ModelAndView modelAndView = new ModelAndView("index"); modelAndView.addObject("username", "lisi"); return modelAndView; } /* * Map */ @RequestMapping(value = "/testmap") public String testmap(Map<String, Object> map) { map.put("age", 18); return "index"; } /* * Model */ @RequestMapping(value = "/testmodel") public String testModel(Model model) { model.addAttribute("email", "1@1.com"); return "index"; } /* * @SessionAttributes */ @RequestMapping(value = "/testsession") public String testsession(Model model) { model.addAttribute("username", "wangwu"); return "index"; } /* * @ModelAttribute */ @ModelAttribute public void model( @RequestParam(value = "userId", required = false) Integer userId, Model model) { if (userId != null) { // 从数据库获取对象 TUser user = new TUser(userId, "wangwu", "123456", "1@1.com", 18); model.addAttribute("user", user); } } @RequestMapping(value = "/testmodelattribute") public String testmodelattribute(@ModelAttribute(value = "user") TUser user) { System.out.println(user); // 数据库的操作 return "index"; } /* * 获取JSON数据 */ @ResponseBody @RequestMapping(value = "/testjson") public TUser testjson() { TUser user = new TUser(5, "wangwu", "123456", "1@1.com", 18); return user; } /* * 文件上传 */ @RequestMapping(value = "/testupload") public void upload(@RequestParam(name = "file1") MultipartFile file) { File newFile = new File("E:\" + file.getOriginalFilename()); if (!newFile.exists()) { newFile.mkdirs(); } try { file.transferTo(newFile); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * servlet api */ @RequestMapping(value = "/testhttpservletrequest") public String testHttpServletRequest(HttpServletRequest request) { request.setAttribute("username", "admin"); return "index"; } /* * redirect */ @RequestMapping(value = "/testredirect") public String testRedirect() { return "redirect:/router/testhttpservletrequest"; } /* * testValidator */ @RequestMapping(value = "/testvalidator") public String testValidator(@Valid TUser user, BindingResult result, HttpServletRequest request) { if (result.hasErrors()) { List<FieldError> fieldErrors = result.getFieldErrors(); for (FieldError fieldError : fieldErrors) { request.setAttribute("_error" + fieldError.getField(), fieldError.getDefaultMessage()); } } return "index"; } }