對於SpringMVC來講,controller由兩個部分構成,分別是分發器和控制器,分發器DispatcherServlet決定着請求使用哪一個控制器,而且決定着控制器返回哪一個視圖,總體結構以下. html
對於DispatcherServlet這個是springMVC框架自動實現,而咱們只須要寫相應的控制器便可,就拿上一個helloworld例子來講,建立一個控制器,只須要給其加上@controller的註解java
/** * 加上@Controller決定這個類是一個控制器 */ @Controller public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(){ return "hello"; } }
@Controller 用於標記在一個類上,使用它標記的類就是一個SpringMVC Controller 對象。分發處理器將會掃描使用了該註解的類的方法,並檢測該方法是否使用了@RequestMapping 註解。@Controller 只是定義了一個控制器類,而使用@RequestMapping 註解的方法纔是真正處理請求的處理器,這個接下來就會講到。
讓控制器起做用,則須要在springMVC.xml配置文件中配置,也就是上一篇搭建基本環境中的配置 web
@RequestMapping 註解決定着返回的jsp視圖,對於這個屬性先簡單來講通常配置value和method兩個值,value決定着請求的路徑,method決定着請求的方法spring
@Controller
public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(){ return "hello"; } }
拿上面例子來講,直接在瀏覽器訪問localhost:8888/hello便可訪問到這個控制器,對應的請求爲GET請求,此方法返回一個hello字符串,說明對應的視圖爲hello.jsp,這是最簡單的訪問形式,若是咱們想要傳值的話,該怎麼作呢?瀏覽器
@RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(Model model){ // 這樣放參數的話,在jsp中直接用EL訪問hello便可 model.addAttribute("hello","world1"); // 這樣方參數的話,默認的key是參數類型 model.addAttribute("world2"); return "hello"; }
在對應的jsp中能夠以下獲取session
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1 style="text-align: center">Hello World!${hello}----${string}</h1> </body> </html>
@RequestMapping(value = "/hello",method = RequestMethod.GET) public ModelAndView hello(){ ModelAndView model = new ModelAndView(); //設置返回視圖名稱 model.setViewName("hello"); //傳值,規則同上 model.addObject("hello","world1"); //傳值,規則同上 model.addObject("world2"); return model; }
兩種方法幾乎沒區別,根據愛好使用. mvc
URI模板是用來獲取url中的值,看下面小例子app
@RequestMapping(value = "/hello/{id}",method = RequestMethod.GET) public String hello(@PathVariable("id") String id, Model model){ // 這樣放參數的話,在jsp中直接用EL訪問hello便可 model.addAttribute("hello",id); return "hello"; }
上面在value = 「/hello/{id}」,說明訪問的url必須爲/hello/XX這樣的連接,@PathVariable(「id」) String id,的做用就是把這個XX放入id中,而後jsp頁面就能夠獲取這個值了 框架
除此以外還支持*通配符訪問jsp
@Controller @RequestMapping ( "/myTest" ) public class MyController { @RequestMapping ( "*/wildcard" ) public String testWildcard() { System. out .println( "wildcard------------" ); return "wildcard" ; } }
那麼此時的訪問路徑只要爲/mytest/**/wildcard均可以訪問當前鏈接
對於請求名和屬性名一致的,springmvc會默認自動綁定,對於不一致的須要使用RequestParam 來進行映射
@RequestMapping ( "requestParam" ) public String testRequestParam( @RequestParam(required=false) String name, @RequestParam ( "age" ) int age) { return "requestParam" ; }
requird=false表明name是非必須值,能夠不存在HttpServletRequest 之中,然後面的age則不一樣,要求必須在HttpServletRequest 之中,不然會報錯.
值得注意的是和@PathVariable 同樣,當你沒有明確指定從request 中取哪一個參數時,Spring 在代碼是debug 編譯的狀況下會默認取更方法參數同名的參數,若是不是debug 編譯的就會報錯。此外,當須要從request 中綁定的參數和方法的參數名不相同的時候,也須要在@RequestParam 中明確指出是要綁定哪一個參數。
相似的還有@CookieValue,@RequestHeader都相似用法,不過通常不用這種方法,接着看下面RequestMapping 的高級用法.
對於方法參數,就像使用Model傳值同樣,能夠直接在參數中加入req,resp,session等,而後就像servlet那樣使用就能夠了
@RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session){ // 這樣放參數的話,在jsp中直接用EL訪問hello便可 return "hello"; }
在經過處理器方法參數接收 request 請求參數綁定數據的時候,對於一些簡單的數據類型 Spring 會幫咱們自動進行類型轉換,而對於一些複雜的類型因爲 Spring 無法識別,因此也就不能幫助咱們進行自動轉換了,這個時候若是咱們須要 Spring 來幫咱們自動轉換的話就須要咱們給 Spring 註冊一個對特定類型的識別轉換器。 Spring 容許咱們提供兩種類型的識別轉換器,一種是註冊在 Controller 中的,一種是註冊在 SpringMVC 的配置文件中。聰明的讀者看到這裏應該能夠想到它們的區別了,定義在 Controller 中的是局部的,只在當前 Controller 中有效,而放在 SpringMVC 配置文件中的是全局的,全部 Controller 均可以拿來使用。
@InitBinder public void initBinder(WebDataBinder binder){ DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); PropertyEditor propertyEditor = new CustomDateEditor(format, true ); // 第二個參數表示是否容許爲空 binder.registerCustomEditor(Date.class, propertyEditor); }
public class MyWebBindingInitializer implements WebBindingInitializer { @Override public void initBinder(WebDataBinder binder, WebRequest request) { // TODO Auto-generated method stub DateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd" ); PropertyEditor propertyEditor = new CustomDateEditor(dateFormat, true ); binder.registerCustomEditor(Date. class , propertyEditor); } }
須要在springMVC.xml中引入這個轉換器
< bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > < property name = "webBindingInitializer" > < bean class = "com.host.app.web.util.MyWebBindingInitializer" /> </ property > </ bean >
剩下的用法沒區別了