先看一個簡單的實例:java
@Controller @RequestMapping("/hello") public class anyTypeController{ @RequestMapping(method={RequestMethod.GET,RequestMethod.POST}) public String processWebRequest(){ return "hello"; } }
上述例子中簡單了闡明瞭Controller以及RequestMapping的用法。含義是web
將這個class申明爲一個bean,當web請求爲/hello時,跳轉到這個class進行處理。spring
Controller的原型分析:
既然上文說了,此處的不須要再配置文件中配置例如<bean id="xxx" class="com.xxx">的語句,可是咱們的Spring如何知道哪些class是bean文件,哪些class文件不是bean文件呢? 按照Spring教材上所講,此處全部配置了@Controller 的class文件回經過java的反射機制進行讀取,所以在這裏Spring2.5官方的 org.springframework.web.servlet.mvc.annotation.defaultAnnotationHandlerMapping 這個類進行處理。Ps:這裏想進一步學習的同窗能夠查看源碼進行分析。這個類的做用是:首先掃描Classpath獲取註解了@Controller的對 象(掃面經過語句:<context:component-sacn/>進行掃描)。 以後經過反射機制進行綁定。
mvc
自定義用於基於註解的HandlerAdapter:app
上面咱們已經經過註解的方式配置好了bean,可是咱們bean中方法不僅是一個,咱們怎麼知道每次訪問的是Controller的哪個具體的方 法呢?具體方法是使用@RequestMapping()註解,而後利用 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 的反射機制,將Controller中的具體方法和請求方法名綁定起來。Ps:想了解更深的同窗請看Spring的源碼。框架
@RequestMapping
函數
因爲在網上搜集到的@RequestMapping的資料很不齊全,看完雲裏霧裏,因而找了本書總結了一下。
學習
@RequestMapping能夠被標註類型申明和方法申明這兩種方式上。ui
申明在類型定義上,那麼@RequestMapping() 括號裏面的東西一般爲請求的地址
spa
申明在方法定義上,那麼它用來代表當面方法定義是一個Web請求處理方法。(Ps:這是書上講的,我的理解的是加上具體的映射條件,使映射不會出錯)
下來看個例子,詳細說明:
@Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(method=RequestMethod.GET) public String function1(..){..} @RequestMapping(method=RequestMethod.POST) public String function2(..){..} }
類型定義上標註的@RequestMapping("/hello")的意思是,全部提交到/hello的web請求都有MyController處理。 可是問題來了,假設這個bean裏面有不少個函數,到底決定由哪個來處理呢?接下來會使用在方法定義上的@RequestMapping()進行進一步 的縮小範圍。如次因此,全部GET方法請求都會被function1處理,POST請求被function2處理。除了method能夠區分,咱們還有 param能夠區分(由於一個bean裏面也不單單隻有兩個函數。GET和POST並不能使用於全部的地方)。
params屬性的兩種表達式形式:
1.「參數名=參數值」,一般參數名相同,後面的參數值用來區分而且界定請求的處理範圍。實例:
@Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(params="locale=en",{method=RequestMethod.POST}) public String function1(..){..} @RequestMapping(params="locale=ch",method={RequestMethod.POST}) public String function2(..){..} }
這裏,當請求爲http://xxx/xxx/hello?locale=en 時,調用function1(..)函數
當請求是http://xxx/xxx/hello?locale=ch時,調用function2()函數
2.「paramter」 形式的表達式。 這裏判斷請求中時候存在某一個參數來決定當前請求交個哪一個方法處理。
@Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(params="delete",{method=RequestMethod.POST}) public String function1(..){..} @RequestMapping(params="update",method={RequestMethod.POST}) public String function2(..){..} }
這裏請求能夠是http://xxxxxxx/hell?delete 就會訪問function1
@RequestMapping還有一一種所有用於方法級別的綁定方式.例:注意,這裏用的是value
@Controller public class MyController{ @RequestMapping("/hello1") public String function1(..){...} @RequestMapping(value="/hello2",{method=RequestMethod.POST}) public String function2(..){..} @RequestMapping(value="/hello3",method={RequestMethod.POST}) public String function3(..){..} }
上述僅僅講了映射的相關知識。下來看若是帶有參數的請求應該如何處理。
請求參數到方法參數的綁定
1.默認綁定行爲
假設有以下請求:<a href="/hello?age=10?name=jack">
@Controller public class MyController{ @RequestMapping("/hello") pulbic String function1(int age ,String name){ System.out.println(age + name); return "success"; } }
在這裏會發現,age 和name自動賦給了函數參數中的age和name。 這是覺得默認綁定行爲是根據名稱匹配原則進行的函數綁定。當請求中的參數名與方法中的參數名一致,相應的參數值將被綁定到對應的方法參數上(這裏框架類保證了請求參數完成了類型轉換)。
2.使用@RequestParam明確的指定綁定關係.一樣是上面的例子.
@Controller public class MyController{ @RequestMapping("/hello") pulbic String function1(@RequestParam("ageOfJack") int age ,String name){ System.out.println(age + name); return "success"; } }
這時候一樣是剛纔的web請求,已經能夠將age的值賦給了ageOfJack參數。默認狀況下,若是@RequestParam請求的數據不存在,會拋 出異常。這是由於他的required屬性決定的。他的默認值是true.假設將他設置爲false,即該參數不存在時不會報錯,對應方法將獲取到該類型 的默認值。
pulbic String function1(@RequestParam("ageOfJack",required=false) int age ,String name)
3.添加自定義數據綁定規則。
Spring框架提供的數據綁定才用JavaBean規範的PropertyEditor機制進行數據轉換。在基於註解的Controller中,能夠用下面兩種方式達到這一目的.
1.使用@InitBinder(針對一個Controller)
2.指定自定義的WebBindingInitialize。(這個是針對多個Controller)
這個感受用的比較少,留下記號,遇到再看。