Spring?MVC中@Controller和@RequestMapping注解使用
@Controller和@RequestMapping注解使用
RequestMapping 注解類型
1)方法級別注解
@RequestMapping(value = "/index/login") public String login() { /** * login代表邏輯視圖名稱,需要根據(jù)Spring MVC配置 * 文件中internalResourceViewResolver的前綴和后綴找到對應(yīng)的物理視圖 */ return "login"; }
2)類級別注解
@RequestMapping("/index") public class IndexController { }
通過 @RequestParam 接收請求參數(shù)
@RequestMapping("/register") /** * 通過@RequestParam接收請求參數(shù) */ public String register(@RequestParam String uname, ? ? @RequestParam String upass, Model model) { ? ? if ("zhangsan".equals(uname) && "123456".equals(upass)) { ? ? ? ? logger.info("成功"); ? ? ? ? return "login"; // 注冊成功,跳轉(zhuǎn)到 login.jsp ? ? } else { ? ? ? ? // 在register.jsp頁面上可以使用EL表達(dá)式取出model的uname值 ? ? ? ? model.addAttribute("uname", uname); ? ? ? ? return "register"; // 返回 register.jsp ? ? } }
@RequestMapping("/register") public String register(@ModelAttribute("user") UserForm user) { ? ? if ("zhangsan".equals(uname) && "123456".equals(upass)) { ? ? ? ? logger.info("成功"); ? ? ? ? return "login"; // 注冊成功,跳轉(zhuǎn)到 login.jsp ? ? } else { ? ? ? ? logger.info("失敗"); ? ? ? ? // 使用@ModelAttribute("user")與model.addAttribute("user",user)的功能相同 ? ? ? ? //register.jsp頁面上可以使用EL表達(dá)式${user.uname}取出ModelAttribute的uname值 ? ? ? ? return "register"; // 返回 register.jsp ? ? } }
Spring MVC應(yīng)用@Autowired和@Service進(jìn)行依賴注入
<context:component-scan base-package="service" />
通過 org.springframework.web.bind.annotation.ModelAttribute
注解類型可經(jīng)常實現(xiàn)以下兩個功能:
1.綁定請求參數(shù)到實體對象(表單的命令對象)
@RequestMapping("/register") public String register(@ModelAttribute("user") UserForm user) { ? ? if ("zhangsan".equals(uname) && "123456".equals(upass)) { ? ? ? ? logger.info("成功"); ? ? ? ? return "login"; ? ? } else { ? ? ? ? logger.info("失敗"); ? ? ? ? return "register"; }
在上述代碼中“@ModelAttribute("user")UserForm user”語句的功能有兩個:
將請求參數(shù)的輸入封裝到 user 對象中。
創(chuàng)建 UserForm 實例。
以“user”為鍵值存儲在 Model 對象中,和“model.addAttribute("user",user)”語句的功能一樣。如果沒有指定鍵值,即“@ModelAttribute UserForm user”,那么在創(chuàng)建 UserForm 實例時以“userForm”為鍵值存儲在 Model 對象中,和“model.addAtttribute("userForm", user)”語句的功能一樣。
2.注解一個非請求處理方法
被 @ModelAttribute 注解的方法將在每次調(diào)用該控制器類的請求處理方法前被調(diào)用。這種特性可以用來控制登錄權(quán)限,當(dāng)然控制登錄權(quán)限的方法有很多,例如攔截器、過濾器等。
攔截器的配置
讓自定義的攔截器生效需要在 Spring MVC 的配置文件中進(jìn)行配置,配置示例代碼如下:
<!-- 配置攔截器 --> <mvc:interceptors> ? ? <!-- 配置一個全局?jǐn)r截器,攔截所有請求 --> ? ? <bean class="interceptor.TestInterceptor" />? ? ? <mvc:interceptor> ? ? ? ? <!-- 配置攔截器作用的路徑 --> ? ? ? ? <mvc:mapping path="/**" /> ? ? ? ? <!-- 配置不需要攔截作用的路徑 --> ? ? ? ? <mvc:exclude-mapping path="" /> ? ? ? ? <!-- 定義<mvc:interceptor>元素中,表示匹配指定路徑的請求才進(jìn)行攔截 --> ? ? ? ? <bean class="interceptor.Interceptor1" /> ? ? </mvc:interceptor> ? ? <mvc:interceptor> ? ? ? ? <!-- 配置攔截器作用的路徑 --> ? ? ? ? <mvc:mapping path="/gotoTest" /> ? ? ? ? <!-- 定義在<mvc: interceptor>元素中,表示匹配指定路徑的請求才進(jìn)行攔截 --> ? ? ? ? <bean class="interceptor.Interceptor2" /> ? ? </mvc:interceptor> </mvc:interceptors>?
在上述示例代碼中,<mvc:interceptors> 元素用于配置一組攔截器,其子元素 <bean> 定義的是全局?jǐn)r截器,即攔截所有的請求。
<mvc:interceptor> 元素中定義的是指定路徑的攔截器,其子元素 <mvc:mapping> 用于配置攔截器作用的路徑,該路徑在其屬性 path 中定義。
如上述示例代碼中,path 的屬性值“/**”表示攔截所有路徑,“/gotoTest”表示攔截所有以“/gotoTest”結(jié)尾的路徑。如果在請求路徑中包含不需要攔截的內(nèi)容,可以通過 <mvc:exclude-mapping> 子元素進(jìn)行配置。
需要注意的是,<mvc:interceptor> 元素的子元素必須按照 <mvc:mapping.../>、<mvc:exclude-mapping.../>、<bean.../> 的順序配置。
@RequestMapping和Controller方法返回值
@RequestMapping
通過@RequestMapping注解可以定義不同的處理器映射規(guī)則。
1. URL路徑映射
@RequestMapping(value="/item") 或 @RequestMapping("/item") --當(dāng)括號里有多個屬性時,value=不可以省略。
value的值是數(shù)組,可以將多個url映射到同一個方法。
/** * 查詢商品列表 * @return */ @RequestMapping(value = { "itemList", "itemListAll" }) public ModelAndView queryItemList() { // 查詢商品數(shù)據(jù) List<Item> list = this.itemService.queryItemList(); // 創(chuàng)建ModelAndView,設(shè)置邏輯視圖名 ModelAndView mv = new ModelAndView("itemList"); // 把商品數(shù)據(jù)放到模型中 mv.addObject("itemList", list); return mv; }
2. 添加在類上面
在class上添加@RequestMapping(url)指定通用請求前綴,限制此類下的所有方法請求url必須以請求前綴開頭
可以使用此方法對url進(jìn)行分類管理,如下圖:
此時需要進(jìn)入queryItemList()方法的請求url為:http://127.0.0.1:8080/api/item/itemList.action
或者
http://127.0.0.1:8080/api/item/itemListAll.action
3.請求方法限定
除了可以對url進(jìn)行設(shè)置,還可以限定請求進(jìn)來的方法。不寫的話默認(rèn)所有方法都可以。
(1)限定GET方法
@RequestMapping(method = RequestMethod.GET)
如果通過POST訪問則報錯:
HTTP Status 405 - Request method 'POST' not supported
例如:
@RequestMapping(value = "itemList",method = RequestMethod.POST)
(2)限定POST方法
@RequestMapping(method = RequestMethod.POST)
如果通過GET訪問則報錯:
HTTP Status 405 - Request method 'GET' not supported
(3)GET和POST都可以
@RequestMapping(method = {RequestMethod.GET,RequestMethod.POST})
Controller方法返回值
1. 返回ModelAndView --無敵的,帶著數(shù)據(jù),返回視圖路徑
controller方法中定義ModelAndView對象并返回,對象中可添加model數(shù)據(jù)、指定view。
@RequestMapping(value = "/item/itemlist.action") public ModelAndView itemList(){ // 從MySQL中查詢數(shù)據(jù) List<Items> list = itemService.selectItemsList(); // 創(chuàng)建ModelAndView,用來存放數(shù)據(jù)和視圖 ModelAndView mav = new ModelAndView(); // 設(shè)置數(shù)據(jù)到模型中 mav.addObject("itemList", list); //頁面上循環(huán)的是itemList // 設(shè)置視圖的路徑,需要設(shè)置視圖的物理地址 //mav.setViewName("/WEB-INF/jsp/itemList.jsp"); mav.setViewName("itemList"); //在springmvc.xml中替換掉默認(rèn)的視圖解析器 return mav; }
2. 返回void --數(shù)據(jù)通過形參 Model model 或者 ModelMap model,但是沒辦法return視圖。
如果需要返回視圖得通過request或response。這種比較適合ajax請求,但是給ajax返回數(shù)據(jù)得json格式數(shù)據(jù)。
在Controller方法形參上可以定義request和response,使用request或response指定響應(yīng)結(jié)果:
(1)使用request轉(zhuǎn)發(fā)頁面,如下:
request.getRequestDispatcher("頁面路徑").forward(request, response);
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
(2)可以通過response頁面重定向:
response.sendRedirect("url")
response.sendRedirect("/itemEdit.action");
(3)可以通過response指定響應(yīng)結(jié)果,例如響應(yīng)json數(shù)據(jù)如下:
response.getWriter().print("{\"abc\":123}");
代碼:
/** * 返回void測試 * * @param request * @param response * @throws Exception */ @RequestMapping("queryItem") public void queryItem(HttpServletRequest request, HttpServletResponse response) throws Exception { // 1 使用request進(jìn)行轉(zhuǎn)發(fā) // request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, // response); // 2 使用response進(jìn)行重定向到編輯頁面 // response.sendRedirect("/springmvc-web2/itemEdit.action"); // 3 使用response直接顯示 response.getWriter().print("{\"abc\":123}"); }
【注意】:改映射@RequestMapping、改方法名、改方法形參、改返回值都需要重啟程序,其他不需要重啟。
3.返回字符串(官方推薦此種方式) --返回視圖路徑,數(shù)據(jù)通過形參 Model model 或者 ModelMap model
(1)邏輯視圖名
controller方法返回字符串可以指定邏輯視圖名,通過視圖解析器解析為物理視圖地址。
//指定邏輯視圖名,經(jīng)過視圖解析器解析為jsp物理路徑:/WEB-INF/jsp/itemList.jsp return "itemList";
示例代碼:
/** * 根據(jù)id查詢商品,綁定簡單數(shù)據(jù)類型 * * @param id * @param model * @return */ @RequestMapping("/itemEdit.action") public String queryItemById(Integer id, ModelMap model) { //不要用int,萬一傳進(jìn)來一個long // 根據(jù)id查詢商品數(shù)據(jù) Items item = this.itemService.selectItemsById(id); // 把商品數(shù)據(jù)放在模型中 model.addAttribute("item", item); return "editItem"; }
(2)Redirect重定向
Contrller方法返回字符串可以重定向到一個url地址。
如下商品修改提交后重定向到商品編輯頁面。
/** * 更新商品 * * @param item * @return */ @RequestMapping("updateItem") public String updateItemById(Items item) { // 更新商品 this.itemService.updateItemById(item); // 修改商品成功后,重定向到商品編輯頁面 // 重定向后瀏覽器地址欄變更為重定向的地址, // 重定向相當(dāng)于執(zhí)行了新的request和response,所以之前的請求參數(shù)都會丟失 // 如果要指定請求參數(shù),需要在重定向的url后面添加 ?itemId=1 這樣的請求參數(shù) return "redirect:/itemEdit.action?itemId=" + item.getId(); }
(3)forward轉(zhuǎn)發(fā)
Controller方法執(zhí)行后繼續(xù)執(zhí)行另一個Controller方法
如下商品修改提交后轉(zhuǎn)向到商品修改頁面,修改商品的id參數(shù)可以帶到商品修改方法中。
/** * 更新商品 * * @param item * @return */ @RequestMapping("updateItem") public String updateItemById2(Items item) { // 更新商品 this.itemService.updateItemById(item); // 修改商品成功后,重定向到商品編輯頁面 // 重定向后瀏覽器地址欄變更為重定向的地址, // 重定向相當(dāng)于執(zhí)行了新的request和response,所以之前的請求參數(shù)都會丟失 // 如果要指定請求參數(shù),需要在重定向的url后面添加 ?itemId=1 這樣的請求參數(shù) // return "redirect:/itemEdit.action?itemId=" + item.getId(); // 修改商品成功后,繼續(xù)執(zhí)行另一個方法 // 使用轉(zhuǎn)發(fā)的方式實現(xiàn)。轉(zhuǎn)發(fā)后瀏覽器地址欄還是原來的請求地址, // 轉(zhuǎn)發(fā)并沒有執(zhí)行新的request和response,所以之前的請求參數(shù)都存在 return "forward:/itemEdit.action"; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家?!?/p>
相關(guān)文章
Spring Boot集成Druid數(shù)據(jù)庫連接池
這篇文章主要介紹了Spring Boot集成Druid數(shù)據(jù)庫連接池,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04mybatis中一對一關(guān)系association標(biāo)簽的使用
這篇文章主要介紹了mybatis中一對一關(guān)系association標(biāo)簽的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03spring mvc中注解@ModelAttribute的妙用分享
這篇文章主要給大家介紹了關(guān)于spring mvc中注解@ModelAttribute妙用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09mybatis模糊查詢之bind標(biāo)簽和concat函數(shù)用法詳解
大家都知道bind 標(biāo)簽可以使用 OGNL 表達(dá)式創(chuàng)建一個變量井將其綁定到上下文中,接下來通過本文給大家介紹了mybatis模糊查詢——bind標(biāo)簽和concat函數(shù)用法,需要的朋友可以參考下2022-08-08