spring mvc4中相關(guān)注解的詳細(xì)講解教程
前言
在開(kāi)始本文之前要說(shuō)明以下,首先我是一個(gè)初學(xué)springmvc,抱著去加深印象的目的去整理相關(guān)springmvc4的相關(guān)注解,同時(shí)也希望給需要相關(guān)查閱的讀者帶來(lái)幫助,好了,下面話就不多說(shuō)了,一起來(lái)看看詳細(xì)的介紹吧。
1.@Controller
Controller控制器是通過(guò)服務(wù)接口定義的提供訪問(wèn)應(yīng)用程序的一種行為,它解釋用戶的輸入,將其轉(zhuǎn)換成一個(gè)模型然后將試圖呈獻(xiàn)給用戶。Spring MVC 使用 @Controller 定義控制器,它還允許自動(dòng)檢測(cè)定義在類(lèi)路徑下的組件并自動(dòng)注冊(cè)。如想自動(dòng)檢測(cè)生效,需在xml頭文件下引入 spring-context:
<?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 注解將類(lèi)似 "/admin"這樣的URL映射到整個(gè)類(lèi)或特定的處理方法上。一般來(lái)說(shuō),類(lèi)級(jí)別的注解映射特定的請(qǐng)求路徑到表單控制器上,而方法級(jí)別的注解只是映射 為一個(gè)特定的HTTP方法請(qǐng)求("GET","POST"等)或HTTP請(qǐng)求參數(shù)。
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的訪問(wèn)地址應(yīng)該是:localhost:8080/proj/admin/login.html
consumes-指定處理請(qǐng)求的提交內(nèi)容類(lèi)型Content-Type,例如 application/json,text/html。
produces-指定返回的內(nèi)容類(lèi)型,僅當(dāng)request請(qǐng)求頭中的(Accept)類(lèi)型中包含該指定類(lèi)型才返回。
value-指定請(qǐng)求的實(shí)際地址,指定的地址可以是URI Template 模式
A) 可以指定為普通的具體值;
B) 可以指定為含有某變量的一類(lèi)值(URI Template Patterns with Path Variables);
C) 可以指定為含正則表達(dá)式的一類(lèi)值( URI Template Patterns with Regular Expressions);
如下示例:
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中必須包含某些參數(shù)值是,才讓該方法處理。
headers-指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求。
如下示例:
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”請(qǐng)求頭和對(duì)應(yīng)值為“http://www.ttyouni.com/”的請(qǐng)求 @RequestMapping(value = "image", headers="Referer=http://www.ttyouni.com/" ) public String getImage(){ return "/WEB-INF/jsp/image.jsp"; } }
3.@RathVariable
在Spring MVC中,可以使用 @PathVariable 注解方法參數(shù)并將其綁定到URI模板變量的值上,之前示例中也有相關(guān)體現(xiàn)。
4.@RequestParam
@RequestParam將請(qǐng)求的參數(shù)綁定到方法中的參數(shù)上。其實(shí)即使不配置該參數(shù),注解也會(huì)默認(rèn)使用該參數(shù)。如果想自定義指定參數(shù)的話,可以將@RequestParam的 required 屬性設(shè)置為false。
5.@RequestBody
@RequestBody是指方法參數(shù)應(yīng)該被綁定到HTTP請(qǐng)求Body上。
6.@SessionAttibutes
@SessionAttibutes可以通過(guò)ModelMap對(duì)象的put操作設(shè)置相關(guān)的session同時(shí)在attibute對(duì)象也會(huì)有該對(duì)象。
示例如下:
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類(lèi)似,它的作用是將返回類(lèi)型直接輸入到HTTP response body中。@ResponseBody在輸出JSON格式的數(shù)據(jù)時(shí)會(huì)用到。
示例如下:
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
我們經(jīng)常見(jiàn)到一些控制器實(shí)現(xiàn)了REST的API,只為服務(wù)于json,xml或其它自定義的類(lèi)型內(nèi)容。@RestController用來(lái)創(chuàng)建REST類(lèi)型的控制器,與@Controller類(lèi)型。@RestController就是這樣一種類(lèi)型,它避免了你重復(fù)的寫(xiě)@RequestMapping與@ResponseBody
9.@ModelAttribute
@ModelAttribute可以作用在方法或方法參數(shù)上,當(dāng)它作用在方法上時(shí),標(biāo)明該方法的目的是添加一個(gè)或多個(gè)模型屬性。
當(dāng)作用在方法參數(shù)上時(shí),表明該參數(shù)可以在方法模型中檢索到。如果該參數(shù)不在當(dāng)前模型中,該參數(shù)先被實(shí)例化然后添加到模型中。一旦模型中有了該參數(shù),該參數(shù)的字段應(yīng)該填充所有請(qǐng)求參數(shù)匹配的名稱(chēng)中。這是spring mvc中重要的數(shù)據(jù)綁定機(jī)制,它省去了單獨(dú)解析每個(gè)表單字段的時(shí)間。
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 ... } }
注解的出現(xiàn)終結(jié)了xml配置文件漫天飛的年代,它讓程序擁有更高的可讀性,可配置性與靈活性。給人一種更簡(jiǎn)潔明了的感覺(jué)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
一分鐘掌握J(rèn)ava?Quartz定時(shí)任務(wù)
這篇文章主要為大家介紹了Java?Quartz定時(shí)任務(wù)一分鐘掌握教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Spring中的@Conditional注解實(shí)現(xiàn)分析
這篇文章主要介紹了Spring中的@Conditional注解實(shí)現(xiàn)分析, @Conditional是Spring 4出現(xiàn)的注解,但是真正露出價(jià)值的是Spring Boot的擴(kuò)展@ConditionalOnBean等,需要的朋友可以參考下2023-12-12Java中的布隆過(guò)濾器原理實(shí)現(xiàn)和應(yīng)用
Java中的布隆過(guò)濾器是一種基于哈希函數(shù)的數(shù)據(jù)結(jié)構(gòu),能夠高效地判斷元素是否存在于一個(gè)集合中。它廣泛應(yīng)用于緩存、網(wǎng)絡(luò)協(xié)議、數(shù)據(jù)查詢等領(lǐng)域,在提高程序性能和減少資源消耗方面具有顯著優(yōu)勢(shì)2023-04-04Java開(kāi)發(fā)完整短信驗(yàn)證碼功能的全過(guò)程
利用短信驗(yàn)證碼進(jìn)行身份驗(yàn)證是目前互聯(lián)網(wǎng)眾多產(chǎn)品常用的一種方式,那么這種短信驗(yàn)證功能是如何實(shí)現(xiàn)的呢,下面這篇文章主要給大家介紹了關(guān)于Java開(kāi)發(fā)完整短信驗(yàn)證碼功能的相關(guān)資料,需要的朋友可以參考下2021-10-10springboot websocket集群(stomp協(xié)議)連接時(shí)候傳遞參數(shù)
這篇文章主要介紹了springboot websocket集群(stomp協(xié)議)連接時(shí)候傳遞參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07springboot2+es7使用RestHighLevelClient的示例代碼
本文主要介紹了springboot2+es7使用RestHighLevelClient的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07