經常使用的一些Spring MVC的路由寫法以及參數傳遞方式。html
參考引用:java
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.htmlweb
這是一些測試例子:spring
package com.my.controller; import java.io.PrintWriter; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value="/default") public class TestController { /** * 使用ModelAndView * @return */ @RequestMapping("/") public ModelAndView index() { ModelAndView mv = new ModelAndView(); mv.addObject("message", "This is index!"); mv.setViewName("index"); return mv; } /** * 使用Model * @param model * @return */ @RequestMapping("/index2") public String index2(Model model) { model.addAttribute("message", "This is index2!"); return "index2"; } /** * 直接返回內容 * @return */ @RequestMapping(value="/index3", method=RequestMethod.GET) @ResponseBody public String index3() { return "This is index3!"; } /** * 直接返回對應的index4的view */ @RequestMapping(value="/index4", method=RequestMethod.GET) public void index4() { } /** * 直接返回index5的view,注意方法後綴:Handler * 比較與index4的區別 */ @RequestMapping(value="/index5", method=RequestMethod.GET) public void index5Handler() { } /** * 傳參方式調用 * @param message * @return */ @RequestMapping(value="/index6/{message}", method=RequestMethod.GET) public ModelAndView index6(@PathVariable String message) { ModelAndView view = new ModelAndView("index6"); view.addObject("message", message); return view; } /** * 傳參方式調用,使用Model * 注意方法內的Model是直接addAttribute,沒有key * 區別index6 * @param message * @param model * @return */ @RequestMapping(value="/index7/{message}", method=RequestMethod.GET) public String index7(@PathVariable String message, Model model) { model.addAttribute(message); return "index6"; } /** * 傳參調用 * 指定參數名稱 * @param userName * @param password * @return */ @RequestMapping(value="/index8/{username}/{password}") public ModelAndView index8(@PathVariable("username") String userName, @PathVariable("password") String password) { ModelAndView view = new ModelAndView("index8"); view.addObject("userName", userName); view.addObject("password", password); return view; } /** * 使用request/response取得參數 * @param request * @param response * @return */ @RequestMapping(value="/index9") public String index9(HttpServletRequest request, HttpServletResponse response) { String message = request.getParameter("message"); request.setAttribute("message", message); return "index9"; } /** * 使用@RequestParam方式取得參數 * @param message * @return */ @RequestMapping(value="/index10") public ModelAndView index10(@RequestParam("message") String message) { ModelAndView view = new ModelAndView("index10"); view.addObject("message", message); return view; } /** * 默認傳參方式 * @param message * @param model * @return */ @RequestMapping(value="/index11") public String index11(String message, Model model) { model.addAttribute("message", message); return "index10"; } /** * 傳入PrintWriteer,也能夠傳入@CookieValue * @param out * @param message * @param model * @return */ @RequestMapping(value="/index12") public String index12(PrintWriter out, String message, Model model) { out.println(message); model.addAttribute("message", message); return "index10"; } /** * 日期參數 * @param day * @return */ @RequestMapping(value="/index13") public ModelAndView index13(@RequestParam("day") @DateTimeFormat(pattern="yyyy-MM-dd") Date day) { ModelAndView view = new ModelAndView("index10"); view.addObject("message", day.toString()); return view; } /** * 默認action * @param message * @return */ @RequestMapping public ModelAndView index14(String message) { ModelAndView view = new ModelAndView("index"); view.addObject("message", message); return view; } /** * 限制訪問的地址,必需有參數p=1才能觸發 * @param message * @param model * @return */ @RequestMapping(value="/index15/{message}", params="p=1", method=RequestMethod.GET) public String index15(@PathVariable("message") String message, Model model) { model.addAttribute("message", message); return "index"; } }