<mvc:annotation-driven />java
對包進行掃描,實現註釋驅動Bean定義,同時將bean自動注入容器中使用。即解決了@Controller標識的類的bean的注入和使用。web
會自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean。spring
若是沒有<mvc:annotation-driven/>,那麼全部的Controller可能就沒有解析,沒有相應的Controller就會被default servlet處理。mvc
<context:component-scan base-package="用於掃描的包" />app
啓用自動檢測webapp
標識一個控制器,@Controller註解定義在org.springframework.steretype包中。使用方式: @Controller 或者 @Controller("")。 jsp
org.springframework.steretype包中還包含@Component @Service @Respository三個註解。@Component是通用標註,@Controller標註web控制器,@Service標註Servicec層的服務,@Respository標註DAO層的數據訪問。函數
在類級別上則表示相對路徑網站
在方法級別上則表示訪問路徑ui
請求:(網站域名+web應用名)web應用根目錄+類定義處@RequestMapping+方法定義處@RequestMapping
映射到
物理視圖:webapp根目錄+springmvc.xml配置的prefix+ 控制器方法的返回值 +springmvc.xml配置的sufix
這裏的話是
請求:http://localhost:8080/MySpringMVC/roger/test
映射到
物理視圖webapp/test.jsp就經過控制器上的
注:當方法返回值類型爲void時,controller方法仍是返回一個String類型的值做爲視圖名(默認爲請求名)
例如:http://localhost:8080/MySpringMVC/roger/test 訪問showTest()
@Controller @RequestMapping("/roger") public class TestController { @RequestMapping("/") public String showIndex() { return "index"; } @RequestMapping(value="/test") public String showTest() { return "test"; } @RequestMapping(value="/get") public void showTest() { //do something.. }(默認返回一個String類型的值做爲視圖名(默認爲請求名))
@RequestMapping的value值先後是否有「/」對請求的路徑沒有影響,即value="test" 、"/test"、"/test/"其效果是同樣的。
在一些場景中,請求的url多是符合必定模式的多個值,這時候須要使用Ant 風格通配符來進行限定。
Ant 風格資源地址支持 3 種匹配符:
?:匹配文件名中的一個字符
*:匹配文件名中的任意字符
**: ** 匹配多層路徑
@RequestMapping 支持 Ant 風格的 URL:
– /user/*/createUser: 匹配
/user/aaa/createUser、 /user/bbb/createUser 等 URL
– /user/**/createUser: 匹配
/user/createUser、 /user/aaa/bbb/createUser 等 URL
– /user/createUser??: 匹配
/user/createUseraa、 /user/createUserbb 等 URL
選擇範圍更小,更加具體的進行映射
?>*>**
僅僅是位置關係有差異,映射是隨機的
@RequestMapping(params="action=del"),請求參數包含「action=del",如http://localhost:8080/MySpringMVC/roger/test?action=del
複雜點,例如:
所傳參數中必須包含username, age=20, 以及不能包含名爲pwd的參數
@RequestMapping(value="testParam2",params={"username","age=20","!pwd"}) public String testParam2() { return "test"; }
使用@RequestParam("xx")註解獲取GET請求或POST請求提交的參數,替代request.getParameter("xx")
例如:
訪問連接:http://localhost:8080/MySpringMVC/roger/testParam?username=roger
@RequestMapping(value = "/testParam", method = RequestMethod.GET) public String testParam(HttpServletRequest request, @RequestParam("username") String username) { System.out.println("HttpServletRequest, username:" + request.getParameter("username")); System.out.println("@RequestParam(), username:" + username); return "test"; }
@RequestParam() 來映射請求參數
value值 即請求參數名
required 該參數是否必需。默認爲true
defaultValue請求參數的默認值
處理函數的參數類型必須是對象類型,不然基本數據類型如int等沒法設爲空。
required屬性標註這個參數是不是必需的,默認是true,若是想讓它能夠不存在,那麼就設置爲false。可是請注意,required設置成false的參數對應的處理函數的參數類型必須是對象類型,不然回報錯500!
package com.happyBKs.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @RequestMapping("class") @Controller public class RPTestHandler { String page="successrm"; @RequestMapping("student") public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false) int age) { System.out.println("a student's request has come. username: "+un+", age: "+age); return page; } }
結果會報錯,由於age雖然設置了required爲false,請求參數能夠不包含age,可是在處理函數中對應的參數類型是int,int是基本數據類型,沒法爲空,因此報錯。解決辦法是將int改成Integer。
若是咱們仍然想用基本類型做爲參數類型,那麼能夠用到@RequestParam註解的defaultValue屬性來指定默認值。
@Controller public class RPTestHandler { String page="successrm"; @RequestMapping("student") public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false, defaultValue="0") int age) { System.out.println("a student's request has come. username: "+un+", age: "+age); return page; } }
使用@PathVariable註解提取路徑中的變量值,映射請求URL中的佔位符到控制器方法參數。
例如:
@RequestMapping(value = "/testPathVariable/{username}", method = RequestMethod.GET) public String testPathVariable(@PathVariable String username) { System.out.println("username:" + username); return "test"; }
訪問路徑能夠是:http://localhost:8080/MySpringMVC/roger/testPathVariable/roger_fang
可是/testPathVariable/{username},路徑中username部分不能有'/'。
@Autowired,註釋類型屬於org.springframework.beans.factory.annotation包,註解到字段或方法。
@Service,註釋類型屬於org.springframework.stereotype包,註解到類上,用於指示類是一個服務,在配置文件中還須要添加一個<mvc:component-scan base-package="xxx" />元素來掃描依賴基本包。
例如:
Controller類
public class ProductController { @Autowired privte ProductService productService; //handle method }
ProductService接口
public interface ProductService { //Product處理方法 }
ProductServiceImpl類
@Service public class ProductServiceImpl implements ProductService{ //Product處理方法 }
本例中,@Autowired註解會使一個ProductService實例被注入到ProductController實例中,同時,爲了使ProductServiceImpl類能被spring掃描到,必須爲其標註@Service。
a、註釋方法的參數
用於將輸入或建立的對象添加到Model對象中(若方法中沒有顯示添加)。
@RequestMapping("/saveProduct") public String saveProduct(@ModelAttribute Product product, Model model) { ... }
@ModelAttribute若是未定義鍵值名則自動將類的首字母改成小寫如"product"做爲鍵值名添加到Model對象中。也能夠自定義@ModelAttribute("xxx")。
從Form表單或URL參數中獲取(實際上,不作此註釋也能拿到product對象)。
b、標註一個非請求的處理方法
被@ModelAttribute註解的方法會在每次調用該控制器類的請求處理方法時被調用。帶@ModelAttribute註解的方法能夠返回一個對象或一個void類型,若是返回一個對象免則返回的對象會自動添加到Model中。
如下兩種方式:
@ModelAtrribute public void populateModel(Model model) { model.addAttribute(new Product()) } @ModelAttribute public Product addProduct() { return new Product(); }
使用@ResponseBody將會跳過視圖處理部分,而是調用適合HttpMessageConverter,將返回值寫入輸出流