前言
在開始本文之前要說明以下,首先我是一個初學springmvc,抱著去加深印象的目的去整理相關springmvc4的相關注解,同時也希望給需要相關查閱的讀者帶來幫助,好了,下面話就不多說了,一起來看看詳細的介紹吧。
1.@Controller
Controller控制器是通過服務接口定義的提供訪問應用程序的一種行為,它解釋用戶的輸入,將其轉換成一個模型然后將試圖呈獻給用戶。Spring MVC 使用 @Controller 定義控制器,它還允許自動檢測定義在類路徑下的組件并自動注冊。如想自動檢測生效,需在xml頭文件下引入 spring-context:
1
|
2
3
4
5
6
7
8
9
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = " http://www.springframework.org/schema/beans " xmlns:p = " http://www.springframework.org/schema/p " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xmlns:context = " http://www.springframework.org/schema/context " xmlns:mvc = " http://www.springframework.org/schema/mvc " xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> < context:component-scan base-package = "com.chen" /> </ beans > |
2.@RequestMapping
RequestMapping 注解將類似 "/admin"這樣的URL映射到整個類或特定的處理方法上。一般來說,類級別的注解映射特定的請求路徑到表單控制器上,而方法級別的注解只是映射 為一個特定的HTTP方法請求("GET","POST"等)或HTTP請求參數。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
|
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping ( "admin" ) public class LoginController { @RequestMapping (value = "login" , method = RequestMethod.GET , consumes = "text/html" ) public String toLoginPage(){ return "/WEB-INF/jsp/login.jsp" ; } } |
上述url的訪問地址應該是:localhost:8080/proj/admin/login.html
consumes-指定處理請求的提交內容類型Content-Type,例如 application/json,text/html。
produces-指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回。
value-指定請求的實際地址,指定的地址可以是URI Template 模式
A) 可以指定為普通的具體值;
B) 可以指定為含有某變量的一類值(URI Template Patterns with Path Variables);
C) 可以指定為含正則表達式的一類值( URI Template Patterns with Regular Expressions);
如下示例:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class BlogController { @RequestMapping (value = "blog/{nick}/{year:20\\d{2}}/{month:1|1[0-2]}/{day:[12][0-9]|30|[1-9]}" , method = RequestMethod.GET) public String toBlogPage( @PathVariable String nick, @PathVariable Integer year, @PathVariable Integer month, @PathVariable Integer day){ return "/WEB-INF/jsp/blog.jsp" ; } } |
params-指定request中必須包含某些參數值是,才讓該方法處理。
headers-指定request中必須包含某些指定的header值,才能讓該方法處理請求。
如下示例:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class BlogController { //僅處理request的header中包含了指定“Refer”請求頭和對應值為“ http://www.ttyouni.com/ ”的請求 @RequestMapping (value = "image" , headers= "Referer= http://www.ttyouni.com/ " ) public String getImage(){ return "/WEB-INF/jsp/image.jsp" ; } } |
3.@RathVariable
在Spring MVC中,可以使用 @PathVariable 注解方法參數并將其綁定到URI模板變量的值上,之前示例中也有相關體現。
4.@RequestParam
@RequestParam將請求的參數綁定到方法中的參數上。其實即使不配置該參數,注解也會默認使用該參數。如果想自定義指定參數的話,可以將@RequestParam的 required 屬性設置為false。
5.@RequestBody
@RequestBody是指方法參數應該被綁定到HTTP請求Body上。
6.@SessionAttibutes
@SessionAttibutes可以通過ModelMap對象的put操作設置相關的session同時在attibute對象也會有該對象。
示例如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; 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.SessionAttributes; import com.chen.proj.service.UserService; @Controller @RequestMapping ( "admin" ) @SessionAttributes ( "user" ) public class LoginController { @Resource UserService service; @RequestMapping (value = "doLogin" , method = RequestMethod.POST) public String doLogin( @RequestParam String username , @RequestParam String password, HttpServletRequest request, ModelMap map ){ try { User user = service.doLogin(username, password); map.put( "user" , user); } catch (Exception e) { request.setAttribute( "error" , e.getMessage()); return "/WEB-INF/jsp/login.jsp" ; } return "/WEB-INF/jsp/loginsuccess.jsp" ; } } |
7.@ResponseBody
@ResponseBody與@RequestBody類似,它的作用是將返回類型直接輸入到HTTP response body中。@ResponseBody在輸出JSON格式的數據時會用到。
示例如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.chen.proj.bean.User; @Controller public class JsonController { @ResponseBody @RequestMapping ( "/getJson" ) public User getUserInfo(){ User user = new User(); user.setPassword( "1234" ); user.setUsername( "jsontest" ); return user; } } |
8.@RestController
我們經常見到一些控制器實現了REST的API,只為服務于json,xml或其它自定義的類型內容。@RestController用來創建REST類型的控制器,與@Controller類型。@RestController就是這樣一種類型,它避免了你重復的寫@RequestMapping與@ResponseBody
9.@ModelAttribute
@ModelAttribute可以作用在方法或方法參數上,當它作用在方法上時,標明該方法的目的是添加一個或多個模型屬性。
當作用在方法參數上時,表明該參數可以在方法模型中檢索到。如果該參數不在當前模型中,該參數先被實例化然后添加到模型中。一旦模型中有了該參數,該參數的字段應該填充所有請求參數匹配的名稱中。這是spring mvc中重要的數據綁定機制,它省去了單獨解析每個表單字段的時間。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.chen.proj.bean.User; @Controller public class UserController { @ModelAttribute public User addUser( @RequestParam String number) { return service.findUser(number); } @ModelAttribute public void populateModel( @RequestParam String number, Model model) { model.addAttribute(service.findUser(number)); // add more ... } } |
注解的出現終結了xml配置文件漫天飛的年代,它讓程序擁有更高的可讀性,可配置性與靈活性。給人一種更簡潔明了的感覺。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/chenjianxiang/p/5521516.html