首先來看下類標記:
/** * Created by LiuHuiChao on 2016/3/21. */ @Controller @RequestMapping("/hello") public class HelloMvcController {
/*簡單訪問示例*/ @RequestMapping("/mvc") public String helloMvc() { return "home"; }
/* request方式獲取參數 */ @RequestMapping("/views3") public String viewCourse3(HttpServletRequest request) { Integer courseId = Integer.valueOf(request.getParameter("courseId")); System.out.println(courseId); return "home"; }
/* 本方法處理 /hello/view?courseId=123 */ @RequestMapping(value = "/views", method = RequestMethod.GET) public String viewCourse(@RequestParam("courseId") Integer courseId) { System.out.println(courseId); return "home"; }
/* restful 風格URL示例 */ /* 本方法處理 /hello/view/{courseId} */ @RequestMapping(value = "/views/{courseId}", method = RequestMethod.GET) public String viewCourse2(@PathVariable("courseId") Integer courseId, Mapmodel) { System.out.println("restful風格URL示例測試---" + courseId); // model.put("course",courseId); return "home"; }
/*重定向操作*/ @RequestMapping(value = "/save", method = RequestMethod.POST) public String doSave(@ModelAttribute Course course) { // 再此處進行save操作 return "redirect:views/" + course.getCourseId();// 重定向 }
/*上傳文件示例*/ @RequestMapping(value="/doUpload",method=RequestMethod.POST) public String doUploadFile(@RequestParam("file") MultipartFile file){ if(!file.isEmpty()){ System.out.println("請在這裡寫入對文件的操作"); //寫入文件等操作。。。。 } return "success"; }
另外,還需要在spring mvc的配置文件中加入: