一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMVC基于注解的Controller詳解

SpringMVC基于注解的Controller詳解

2020-07-27 13:41楓之逆 Java教程

這篇文章主要介紹了SpringMVC基于注解的Controller詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

概述

繼 Spring 2.0 對 Spring MVC 進(jìn)行重大升級后,Spring 2.5 又為 Spring MVC 引入了注解驅(qū)動(dòng)功能。現(xiàn)在你無須讓 Controller 繼承任何接口,無需在 XML 配置文件中定義請求和 Controller 的映射關(guān)系,僅僅使用注解就可以讓一個(gè) POJO 具有 Controller 的絕大部分功能 —— Spring MVC 框架的易用性得到了進(jìn)一步的增強(qiáng).在框架靈活性、易用性和擴(kuò)展性上,Spring MVC 已經(jīng)全面超越了其它的 MVC 框架,伴隨著 Spring 一路高唱猛進(jìn),可以預(yù)見 Spring MVC 在 MVC 市場上的吸引力將越來越不可抗拒。

本文將介紹 Spring 2.5 新增的 Sping MVC 注解功能,講述如何使用注解配置替換傳統(tǒng)的基于 XML 的 Spring MVC 配置。

 一個(gè)簡單的基于注解的 Controller

使用過低版本 Spring MVC 的讀者都知道:當(dāng)創(chuàng)建一個(gè) Controller 時(shí),我們需要直接或間接地實(shí)現(xiàn) org.springframework.web.servlet.mvc.Controller 接口。一般情況下,我們是通過繼承 SimpleFormController 或 MultiActionController 來定義自己的 Controller 的。在定義 Controller 后,一個(gè)重要的事件是在 Spring MVC 的配置文件中通過 HandlerMapping 定義請求和控制器的映射關(guān)系,以便將兩者關(guān)聯(lián)起來。

來看一下基于注解的 Controller 是如何定義做到這一點(diǎn)的,下面是使用注解的 BbtForumController:

清單 1. BbtForumController.java

?
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
package com.baobaotao.web;
 
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import java.util.Collection;
 
@Controller          //<——①
@RequestMapping("/forum.do")
public class BbtForumController {
 
  @Autowired
  private BbtForumService bbtForumService;
 
  @RequestMapping //<——②
  public String listAllBoard() {
    bbtForumService.getAllBoard();
    System.out.println("call listAllBoard method.");
    return "listBoard";
  }
}

從上面代碼中,我們可以看出 BbtForumController 和一般的類并沒有區(qū)別,它沒有實(shí)現(xiàn)任何特殊的接口,因而是一個(gè)地道的 POJO。讓這個(gè) POJO 與眾不同的魔棒就是 Spring MVC 的注解!

在 ① 處使用了兩個(gè)注解,分別是 @Controller 和 @RequestMapping。 @Controller、@Service 以及 @Repository 和 @Component 注解的作用是等價(jià)的:將一個(gè)類成為 Spring 容器的 Bean。由于 Spring MVC 的 Controller 必須事先是一個(gè) Bean,所以 @Controller 注解是不可缺少的。

真正讓 BbtForumController 具備 Spring MVC Controller 功能的是 @RequestMapping 這個(gè)注解。@RequestMapping 可以標(biāo)注在類定義處,將 Controller 和特定請求關(guān)聯(lián)起來;還可以標(biāo)注在方法簽名處,以便進(jìn)一步對請求進(jìn)行分流。在 ① 處,我們讓 BbtForumController 關(guān)聯(lián)“/forum.do”的請求,而 ② 處,我們具體地指定 listAllBoard() 方法來處理請求。所以在類聲明處標(biāo)注的 @RequestMapping 相當(dāng)于讓 POJO 實(shí)現(xiàn)了 Controller 接口,而在方法定義處的 @RequestMapping 相當(dāng)于讓 POJO 擴(kuò)展 Spring 預(yù)定義的 Controller(如 SimpleFormController 等)。

為了讓基于注解的 Spring MVC 真正工作起來,需要在 Spring MVC 對應(yīng)的 xxx-servlet.xml 配置文件中做一些手腳。在此之前,還是先來看一下 web.xml 的配置吧:

清單 2. web.xml:啟用 Spring 容器和 Spring MVC 框架               

?
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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Spring Annotation MVC Sample</display-name>
  <!-- Spring 服務(wù)層的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
   
  <!-- Spring 容器啟動(dòng)監(jiān)聽器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
 
 
  <!-- Spring MVC 的Servlet,它將加載WEB-INF/annomvc-servlet.xml 的
  配置文件,以啟動(dòng)Spring MVC模塊-->
  <servlet>
    <servlet-name>annomvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>annomvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

web.xml 中定義了一個(gè)名為 annomvc 的 Spring MVC 模塊,按照 Spring MVC 的契約,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定義 Spring MVC 模塊的具體配置。annomvc-servlet.xml 的配置內(nèi)容如下所示:

清單 3. annomvc-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   
  <!-- ①:對web包中的所有類進(jìn)行掃描,以完成Bean創(chuàng)建和自動(dòng)依賴注入的功能 -->
  <context:component-scan base-package="com.baobaotao.web"/>
 
  <!-- ②:啟動(dòng)Spring MVC的注解功能,完成請求和注解POJO的映射 -->
  <bean class="org.springframework.web.servlet.mvc.annotation.    AnnotationMethodHandlerAdapter"/>
 
  <!-- ③:對模型視圖名稱的解析,即在模型視圖名稱添加前后綴 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>

因?yàn)?Spring 所有功能都在 Bean 的基礎(chǔ)上演化而來,所以必須事先將 Controller 變成 Bean,這是通過在類中標(biāo)注 @Controller 并在 annomvc-servlet.xml 中啟用組件掃描機(jī)制來完成的,如 ① 所示。

在 ② 處,配置了一個(gè) AnnotationMethodHandlerAdapter,它負(fù)責(zé)根據(jù) Bean 中的 Spring MVC 注解對 Bean 進(jìn)行加工處理,使這些 Bean 變成控制器并映射特定的 URL 請求。

而 ③ 處的工作是定義模型視圖名稱的解析規(guī)則,這里我們使用了 Spring 2.5 的特殊命名空間,即 p 命名空間,它將原先需要通過 <property> 元素配置的內(nèi)容轉(zhuǎn)化為 <bean> 屬性配置,在一定程度上簡化了 <bean> 的配置。

啟動(dòng) Tomcat,發(fā)送 http://localhost/forum.do URL 請求,BbtForumController 的 listAllBoard() 方法將響應(yīng)這個(gè)請求,并轉(zhuǎn)向 WEB-INF/jsp/listBoard.jsp 的視圖頁面。

讓一個(gè) Controller 處理多個(gè) URL 請求

在低版本的 Spring MVC 中,我們可以通過繼承 MultiActionController 讓一個(gè) Controller 處理多個(gè) URL 請求。使用 @RequestMapping 注解后,這個(gè)功能更加容易實(shí)現(xiàn)了。請看下面的代碼:

清單 3. 每個(gè)請求處理參數(shù)對應(yīng)一個(gè) URL

?
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
package com.baobaotao.web;
 
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class BbtForumController {
  @Autowired
  private BbtForumService bbtForumService;
 
  @RequestMapping("/listAllBoard.do") // <—— ①
  public String listAllBoard() {
    bbtForumService.getAllBoard();
    System.out.println("call listAllBoard method.");
    return "listBoard";
  }
 
  @RequestMapping("/listBoardTopic.do") // <—— ②
  public String listBoardTopic(int topicId) {
    bbtForumService.getBoardTopics(topicId);
    System.out.println("call listBoardTopic method.");
    return "listTopic";
  }
}

 在這里,我們分別在 ① 和 ② 處為 listAllBoard() 和 listBoardTopic() 方法標(biāo)注了 @RequestMapping 注解,分別指定這兩個(gè)方法處理的 URL 請求,這相當(dāng)于將 BbtForumController 改造為 MultiActionController。這樣 /listAllBoard.do 的 URL 請求將由 listAllBoard() 負(fù)責(zé)處理,而 /listBoardTopic.do?topicId=1 的 URL 請求則由 listBoardTopic() 方法處理。

對于處理多個(gè) URL 請求的 Controller 來說,我們傾向于通過一個(gè) URL 參數(shù)指定 Controller 處理方法的名稱(如 method=listAllBoard),而非直接通過不同的 URL 指定 Controller 的處理方法。使用 @RequestMapping 注解很容易實(shí)現(xiàn)這個(gè)常用的需求。來看下面的代碼:

清單 4. 一個(gè) Controller 對應(yīng)一個(gè) URL,由請求參數(shù)決定請求處理方法

?
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
package com.baobaotao.web;
 
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
@RequestMapping("/bbtForum.do") // <—— ① 指定控制器對應(yīng)URL請求
public class BbtForumController {
 
  @Autowired
  private BbtForumService bbtForumService;
 
  // <—— ② 如果URL請求中包括"method=listAllBoard"的參數(shù),由本方法進(jìn)行處理
  @RequestMapping(params = "method=listAllBoard")
  public String listAllBoard() {
    bbtForumService.getAllBoard();
    System.out.println("call listAllBoard method.");
    return "listBoard";
  }
 
  // <—— ③ 如果URL請求中包括"method=listBoardTopic"的參數(shù),由本方法進(jìn)行處理
  @RequestMapping(params = "method=listBoardTopic")
  public String listBoardTopic(int topicId) {
    bbtForumService.getBoardTopics(topicId);
    System.out.println("call listBoardTopic method.");
    return "listTopic";
  }
}

 在類定義處標(biāo)注的 @RequestMapping 讓 BbtForumController 處理所有包含 /bbtForum.do 的 URL 請求,而 BbtForumController 中的請求處理方法對 URL 請求的分流規(guī)則在 ② 和 ③ 處定義分流規(guī)則按照 URL 的 method 請求參數(shù)確定。所以分別在類定義處和方法定義處使用 @RequestMapping 注解,就可以很容易通過 URL 參數(shù)指定 Controller 的處理方法了。

@RequestMapping 注解中除了 params 屬性外,還有一個(gè)常用的屬性是 method,它可以讓 Controller 方法處理特定 HTTP 請求方式的請求,如讓一個(gè)方法處理 HTTP GET 請求,而另一個(gè)方法處理 HTTP POST 請求,如下所示:

清單 4. 讓請求處理方法處理特定的 HTTP 請求方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.baobaotao.web;
 
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
@RequestMapping("/bbtForum.do")
public class BbtForumController {
 
  @RequestMapping(params = "method=createTopic",method = RequestMethod.POST)
  public String createTopic(){
    System.out.println("call createTopic method.");
    return "createTopic";
  }
}

這樣只有當(dāng) /bbtForum.do?method=createTopic 請求以 HTTP POST 方式提交時(shí),createTopic() 方法才會(huì)進(jìn)行處理。

 處理方法入?yún)⑷绾谓壎?URL 參數(shù)

按契約綁定

Controller 的方法標(biāo)注了 @RequestMapping 注解后,它就能處理特定的 URL 請求。我們不禁要問:請求處理方法入?yún)⑹侨绾谓壎?URL 參數(shù)的呢?在回答這個(gè)問題之前先來看下面的代碼:

清單 5. 按參數(shù)名匹配進(jìn)行綁定

?
1
2
3
4
5
6
7
@RequestMapping(params = "method=listBoardTopic")
 //<—— ① topicId入?yún)⑹侨绾谓壎║RL請求參數(shù)的?
 public String listBoardTopic(int topicId) {
   bbtForumService.getBoardTopics(topicId);
   System.out.println("call listBoardTopic method.");
   return "listTopic";
 }

當(dāng)我們發(fā)送 http://localhost//bbtForum.do?method=listBoardTopic&topicId=10 的 URL 請求時(shí),Spring 不但讓 listBoardTopic() 方法處理這個(gè)請求,而且還將 topicId 請求參數(shù)在類型轉(zhuǎn)換后綁定到 listBoardTopic() 方法的 topicId 入?yún)⑸稀6?listBoardTopic() 方法的返回類型是 String,它將被解析為邏輯視圖的名稱。也就是說 Spring 在如何給處理方法入?yún)⒆詣?dòng)賦值以及如何將處理方法返回值轉(zhuǎn)化為 ModelAndView 中的過程中存在一套潛在的規(guī)則,不熟悉這個(gè)規(guī)則就不可能很好地開發(fā)基于注解的請求處理方法,因此了解這個(gè)潛在規(guī)則無疑成為理解 Spring MVC 框架基于注解功能的核心問題。

我們不妨從最常見的開始說起:請求處理方法入?yún)⒌念愋涂梢允?Java 基本數(shù)據(jù)類型或 String 類型,這時(shí)方法入?yún)磪?shù)名匹配的原則綁定到 URL 請求參數(shù),同時(shí)還自動(dòng)完成 String 類型的 URL 請求參數(shù)到請求處理方法參數(shù)類型的轉(zhuǎn)換。下面給出幾個(gè)例子:

  • listBoardTopic(int topicId):和 topicId URL 請求參數(shù)綁定;
  • listBoardTopic(int topicId,String boardName):分別和 topicId、boardName URL 請求參數(shù)綁定;

特別的,如果入?yún)⑹腔緮?shù)據(jù)類型(如 int、long、float 等),URL 請求參數(shù)中一定要有對應(yīng)的參數(shù),否則將拋出 TypeMismatchException 異常,提示無法將 null 轉(zhuǎn)換為基本數(shù)據(jù)類型。

另外,請求處理方法的入?yún)⒁部梢砸粋€(gè) JavaBean,如下面的 User 對象就可以作為一個(gè)入?yún)ⅲ?/p>

清單 6. User.java:一個(gè) JavaBean               

?
1
2
3
4
5
6
7
8
9
10
package com.baobaotao.web;
 
public class User {
  private int userId;
  private String userName;
  //省略get/setter方法
  public String toString(){
    return this.userName +","+this.userId;
  }
}

下面是將 User 作為 listBoardTopic() 請求處理方法的入?yún)ⅲ?/p>

清單 7. 使用 JavaBean 作為請求處理方法的入?yún)?/p>

?
1
2
3
4
5
6
7
8
   @RequestMapping(params = "method=listBoardTopic")
public String listBoardTopic(int topicId,User user) {
  bbtForumService.getBoardTopics(topicId);
  System.out.println("topicId:"+topicId);
  System.out.println("user:"+user);
  System.out.println("call listBoardTopic method.");
  return "listTopic";
}

這時(shí),如果我們使用以下的 URL 請求:http://localhost/bbtForum.do?ethod=listBoardTopic&topicId=1&userId=10&userName=tom

topicId URL 參數(shù)將綁定到 topicId 入?yún)⑸希?userId 和 userName URL 參數(shù)將綁定到 user 對象的 userId 和 userName 屬性中。和 URL 請求中不允許沒有 topicId 參數(shù)不同,雖然 User 的 userId 屬性的類型是基本數(shù)據(jù)類型,但如果 URL 中不存在 userId 參數(shù),Spring 也不會(huì)報(bào)錯(cuò),此時(shí) user.userId 值為 0。如果 User 對象擁有一個(gè) dept.deptId 的級聯(lián)屬性,那么它將和 dept.deptId URL 參數(shù)綁定。

通過注解指定綁定的 URL 參數(shù)

如果我們想改變這種默認(rèn)的按名稱匹配的策略,比如讓 listBoardTopic(int topicId,User user) 中的 topicId 綁定到 id 這個(gè) URL 參數(shù),那么可以通過對入?yún)⑹褂?@RequestParam 注解來達(dá)到目的:

清單 8. 通過 @RequestParam 注解指定

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.baobaotao.web;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
 
@Controller
@RequestMapping("/bbtForum.do")
public class BbtForumController {
 
  @RequestMapping(params = "method=listBoardTopic")
  public String listBoardTopic(@RequestParam("id") int topicId,User user) {
    bbtForumService.getBoardTopics(topicId);
    System.out.println("topicId:"+topicId);
    System.out.println("user:"+user);
    System.out.println("call listBoardTopic method.");
    return "listTopic";
  }
}

這里,對 listBoardTopic() 請求處理方法的 topicId 入?yún)?biāo)注了 @RequestParam("id") 注解,所以它將和 id 的 URL 參數(shù)綁定。

綁定模型對象中某個(gè)屬性

Spring 2.0 定義了一個(gè) org.springframework.ui.ModelMap 類,它作為通用的模型數(shù)據(jù)承載對象,傳遞數(shù)據(jù)供視圖所用。我們可以在請求處理方法中聲明一個(gè) ModelMap 類型的入?yún)ⅲ琒pring 會(huì)將本次請求模型對象引用通過該入?yún)鬟f進(jìn)來,這樣就可以在請求處理方法內(nèi)部訪問模型對象了。來看下面的例子:

清單 9. 使用 ModelMap 訪問請示對應(yīng)的隱含模型對象

?
1
2
3
4
5
6
7
8
9
10
@RequestMapping(params = "method=listBoardTopic")
public String listBoardTopic(@RequestParam("id")int topicId,
User user,ModelMap model) {
  bbtForumService.getBoardTopics(topicId);
  System.out.println("topicId:" + topicId);
  System.out.println("user:" + user);
  //① 將user對象以currUser為鍵放入到model中
  model.addAttribute("currUser",user);
  return "listTopic";
}

對于當(dāng)次請求所對應(yīng)的模型對象來說,其所有屬性都將存放到 request 的屬性列表中。象上面的例子,ModelMap 中的 currUser 屬性將放到 request 的屬性列表中,所以可以在 JSP 視圖頁面中通過 request.getAttribute(“currUser”) 或者通過 ${currUser} EL 表達(dá)式訪問模型對象中的 user 對象。從這個(gè)角度上看, ModelMap 相當(dāng)于是一個(gè)向 request 屬性列表中添加對象的一條管道,借由 ModelMap 對象的支持,我們可以在一個(gè)不依賴 Servlet API 的 Controller 中向 request 中添加屬性。

在默認(rèn)情況下,ModelMap 中的屬性作用域是 request 級別是,也就是說,當(dāng)本次請求結(jié)束后,ModelMap 中的屬性將銷毀。如果希望在多個(gè)請求中共享 ModelMap 中的屬性,必須將其屬性轉(zhuǎn)存到 session 中,這樣 ModelMap 的屬性才可以被跨請求訪問。

Spring 允許我們有選擇地指定 ModelMap 中的哪些屬性需要轉(zhuǎn)存到 session 中,以便下一個(gè)請求屬對應(yīng)的 ModelMap 的屬性列表中還能訪問到這些屬性。這一功能是通過類定義處標(biāo)注 @SessionAttributes 注解來實(shí)現(xiàn)的。請看下面的代碼:

清單 10. 使模型對象的特定屬性具有 Session 范圍的作用域

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.baobaotao.web;
 
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.SessionAttributes;
 
@Controller
@RequestMapping("/bbtForum.do")
@SessionAttributes("currUser") //①將ModelMap中屬性名為currUser的屬性
//放到Session屬性列表中,以便這個(gè)屬性可以跨請求訪問
public class BbtForumController {
  @RequestMapping(params = "method=listBoardTopic")
  public String listBoardTopic(@RequestParam("id")int topicId, User user,
ModelMap model) {
    bbtForumService.getBoardTopics(topicId);
    System.out.println("topicId:" + topicId);
    System.out.println("user:" + user);
    model.addAttribute("currUser",user); //②向ModelMap中添加一個(gè)屬性
    return "listTopic";
  }
 
}

我們在 ② 處添加了一個(gè) ModelMap 屬性,其屬性名為 currUser,而 ① 處通過 @SessionAttributes 注解將 ModelMap 中名為 currUser 的屬性放置到 Session 中,所以我們不但可以在 listBoardTopic() 請求所對應(yīng)的 JSP 視圖頁面中通過 request.getAttribute(“currUser”) 和 session.getAttribute(“currUser”) 獲取 user 對象,還可以在下一個(gè)請求所對應(yīng)的 JSP 視圖頁面中通過 session.getAttribute(“currUser”) 或 ModelMap#get(“currUser”) 訪問到這個(gè)屬性。

這里我們僅將一個(gè) ModelMap 的屬性放入 Session 中,其實(shí) @SessionAttributes 允許指定多個(gè)屬性。你可以通過字符串?dāng)?shù)組的方式指定多個(gè)屬性,如 @SessionAttributes({“attr1”,”attr2”})。此外,@SessionAttributes 還可以通過屬性類型指定要 session 化的 ModelMap 屬性,如 @SessionAttributes(types = User.class),當(dāng)然也可以指定多個(gè)類,如 @SessionAttributes(types = {User.class,Dept.class}),還可以聯(lián)合使用屬性名和屬性類型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。

上面講述了如何往ModelMap中放置屬性以及如何使ModelMap中的屬性擁有Session域的作用范圍。除了在JSP視圖頁面中通過傳統(tǒng)的方法訪問ModelMap中的屬性外,讀者朋友可能會(huì)問:是否可以將ModelMap中的屬性綁定到請求處理方法的入?yún)⒅心兀看鸢甘强隙ǖ摹pring為此提供了一個(gè)@ModelAttribute的注解,下面是使用@ModelAttribute注解的例子:

清單 11. 使模型對象的特定屬性具有 Session 范圍的作用域

?
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
35
36
37
38
39
40
41
42
package com.baobaotao.web;
 
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.annotation.ModelAttribute;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
@Controller
@RequestMapping("/bbtForum.do")
@SessionAttributes("currUser") //①讓ModelMap的currUser屬性擁有session級作用域
public class BbtForumController {
 
  @Autowired
private BbtForumService bbtForumService;
 
  @RequestMapping(params = "method=listBoardTopic")
  public String listBoardTopic(@RequestParam("id")int topicId, User user,
ModelMap model) {
    bbtForumService.getBoardTopics(topicId);
    System.out.println("topicId:" + topicId);
    System.out.println("user:" + user);
    model.addAttribute("currUser",user); //②向ModelMap中添加一個(gè)屬性
    return "listTopic";
  }
 
 
  @RequestMapping(params = "method=listAllBoard")
  //③將ModelMap中的
public String listAllBoard(@ModelAttribute("currUser") User user) {
//currUser屬性綁定到user入?yún)⒅小?/code>
    bbtForumService.getAllBoard();
    System.out.println("user:"+user);
    return "listBoard";
  }
}

在 ② 處,我們向 ModelMap 中添加一個(gè)名為 currUser 的屬性,而 ① 外的注解使這個(gè) currUser 屬性擁有了 session 級的作用域。所以,我們可以在 ③ 處通過 @ModelAttribute 注解將 ModelMap 中的 currUser 屬性綁定以請求處理方法的 user 入?yún)⒅小?/p>

所以當(dāng)我們先調(diào)用以下 URL 請求: http://localhost/bbtForum.do?method=listBoardTopic&id=1&userName=tom&dept.deptId=12

以執(zhí)行l(wèi)istBoardTopic()請求處理方法,然后再訪問以下URL: http://localhost/sample/bbtForum.do?method=listAllBoard

你將可以看到 listAllBoard() 的 user 入?yún)⒁呀?jīng)成功綁定到 listBoardTopic() 中注冊的 session 級的 currUser 屬性上了。

 請求處理方法的簽名規(guī)約

方法入?yún)?/p>

我們知道標(biāo)注了 @RequestMapping 注解的 Controller 方法就成為了請求處理方法,Spring MVC 允許極其靈活的請求處理方法簽名方式。對于方法入?yún)碚f,它允許多種類型的入?yún)ⅲㄟ^下表進(jìn)行說明:

 

請求處理方法入?yún)⒌目蛇x類型 說明
Java 基本數(shù)據(jù)類型和 String 默認(rèn)情況下將按名稱匹配的方式綁定到 URL 參數(shù)上,可以通過 @RequestParam 注解改變默認(rèn)的綁定規(guī)則
request/response/session 既可以是 Servlet API 的也可以是 Portlet API 對應(yīng)的對象,Spring 會(huì)將它們綁定到 Servlet 和 Portlet 容器的相應(yīng)對象上
org.springframework.web.context.request.WebRequest 內(nèi)部包含了 request 對象
java.util.Locale 綁定到 request 對應(yīng)的 Locale 對象上
java.io.InputStream/java.io.Reader 可以借此訪問 request 的內(nèi)容
java.io.OutputStream / java.io.Writer 可以借此操作 response 的內(nèi)容
任何標(biāo)注了 @RequestParam 注解的入?yún)?/td> 被標(biāo)注 @RequestParam 注解的入?yún)⒔壎ǖ教囟ǖ?request 參數(shù)上。
java.util.Map / org.springframework.ui.ModelMap 它綁定 Spring MVC 框架中每個(gè)請求所創(chuàng)建的潛在的模型對象,它們可以被 Web 視圖對象訪問(如 JSP)
命令/表單對象(注:一般稱綁定使用 HTTP GET 發(fā)送的 URL 參數(shù)的對象為命令對象,而稱綁定使用 HTTP POST 發(fā)送的 URL 參數(shù)的對象為表單對象) 它們的屬性將以名稱匹配的規(guī)則綁定到 URL 參數(shù)上,同時(shí)完成類型的轉(zhuǎn)換。而類型轉(zhuǎn)換的規(guī)則可以通過 @InitBinder 注解或通過 HandlerAdapter 的配置進(jìn)行調(diào)整
org.springframework.validation.Errors / org.springframework.validation.BindingResult 為屬性列表中的命令/表單對象的校驗(yàn)結(jié)果,注意檢驗(yàn)結(jié)果參數(shù)必須緊跟在命令/表單對象的后面
rg.springframework.web.bind.support.SessionStatus 可以通過該類型 status 對象顯式結(jié)束表單的處理,這相當(dāng)于觸發(fā) session 清除其中的通過 @SessionAttributes 定義的屬性

 

Spring MVC 框架的易用之處在于,你可以按任意順序定義請求處理方法的入?yún)ⅲǔ?Errors 和 BindingResult 必須緊跟在命令對象/表單參數(shù)后面以外),Spring MVC 會(huì)根據(jù)反射機(jī)制自動(dòng)將對應(yīng)的對象通過入?yún)鬟f給請求處理方法。這種機(jī)制讓開發(fā)者完全可以不依賴 Servlet API 開發(fā)控制層的程序,當(dāng)請求處理方法需要特定的對象時(shí),僅僅需要在參數(shù)列表中聲明入?yún)⒓纯桑恍枰紤]如何獲取這些對象,Spring MVC 框架就象一個(gè)大管家一樣“不辭辛苦”地為我們準(zhǔn)備好了所需的一切。下面演示一下使用 SessionStatus 的例子:

清單 12. 使用 SessionStatus 控制 Session 級別的模型屬性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Owner owner,
BindingResult result, SessionStatus status) {//<——①
  new OwnerValidator().validate(owner, result);
  if (result.hasErrors()) {
    return "ownerForm";
  }
  else {
    this.clinic.storeOwner(owner);
    status.setComplete();//<——②
    return "redirect:owner.do?ownerId=" + owner.getId();
  }
}

processSubmit() 方法中的 owner 表單對象將綁定到 ModelMap 的“owner”屬性中,result 參數(shù)用于存放檢驗(yàn) owner 結(jié)果的對象,而 status 用于控制表單處理的狀態(tài)。在 ② 處,我們通過調(diào)用 status.setComplete() 方法,該 Controller 所有放在 session 級別的模型屬性數(shù)據(jù)將從 session 中清空。

方法返回參數(shù)

在低版本的 Spring MVC 中,請求處理方法的返回值類型都必須是 ModelAndView。而在 Spring 2.5 中,你擁有多種靈活的選擇。通過下表進(jìn)行說明:

SpringMVC基于注解的Controller詳解

應(yīng)該說使用 String 作為請求處理方法的返回值類型是比較通用的方法,這樣返回的邏輯視圖名不會(huì)和請求 URL 綁定,具有很大的靈活性,而模型數(shù)據(jù)又可以通過 ModelMap 控制。當(dāng)然直接使用傳統(tǒng)的 ModelAndView 也不失為一個(gè)好的選擇。

注冊自己的屬性編輯器

Spring MVC 有一套常用的屬性編輯器,這包括基本數(shù)據(jù)類型及其包裹類的屬性編輯器、String 屬性編輯器、JavaBean 的屬性編輯器等。但有時(shí)我們還需要向 Spring MVC 框架注冊一些自定義的屬性編輯器,如特定時(shí)間格式的屬性編輯器就是其中一例。

Spring MVC 允許向整個(gè) Spring 框架注冊屬性編輯器,它們對所有 Controller 都有影響。當(dāng)然 Spring MVC 也允許僅向某個(gè) Controller 注冊屬性編輯器,對其它的 Controller 沒有影響。前者可以通過 AnnotationMethodHandlerAdapter 的配置做到,而后者則可以通過 @InitBinder 注解實(shí)現(xiàn)。

下面先看向整個(gè) Spring MVC 框架注冊的自定義編輯器:

清單 13. 注冊框架級的自定義屬性編輯器

?
1
2
3
4
5
6
<bean class="org.springframework.web.servlet.mvc.annotation.
AnnotationMethodHandlerAdapter">
  <property name="webBindingInitializer">
    <bean class="com.baobaotao.web.MyBindingInitializer"/>
  </property>
</bean>

MyBindingInitializer 實(shí)現(xiàn)了 WebBindingInitializer 接口,在接口方法中通過 binder 注冊多個(gè)自定義的屬性編輯器,其代碼如下所示:

清單 14.自定義屬性編輯器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package org.springframework.samples.petclinic.web;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.PetType;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
 
public class MyBindingInitializer implements WebBindingInitializer {
 
  public void initBinder(WebDataBinder binder, WebRequest request) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class,
      new CustomDateEditor(dateFormat, false));
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
  }
}

如果希望某個(gè)屬性編輯器僅作用于特定的 Controller,可以在 Controller 中定義一個(gè)標(biāo)注 @InitBinder 注解的方法,可以在該方法中向 Controller 了注冊若干個(gè)屬性編輯器,來看下面的代碼:

清單 15. 注冊 Controller 級的自定義屬性編輯器

?
1
2
3
4
5
6
7
8
9
10
11
        @Controller
public class MyFormController {
 
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
  }
  
}

注意被標(biāo)注 @InitBinder 注解的方法必須擁有一個(gè) WebDataBinder 類型的入?yún)ⅲ员?Spring MVC 框架將注冊屬性編輯器的 WebDataBinder 對象傳遞進(jìn)來。

如何準(zhǔn)備數(shù)據(jù)

在編寫 Controller 時(shí),常常需要在真正進(jìn)入請求處理方法前準(zhǔn)備一些數(shù)據(jù),以便請求處理或視圖渲染時(shí)使用。在傳統(tǒng)的 SimpleFormController 里,是通過復(fù)寫其 referenceData() 方法來準(zhǔn)備引用數(shù)據(jù)的。在 Spring 2.5 時(shí),可以將任何一個(gè)擁有返回值的方法標(biāo)注上 @ModelAttribute,使其返回值將會(huì)進(jìn)入到模型對象的屬性列表中。來看下面的例子:

清單 16. 定義為處理請求準(zhǔn)備數(shù)據(jù)的方法

?
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
35
36
37
38
39
40
package com.baobaotao.web;
 
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
 
@Controller
@RequestMapping("/bbtForum.do")
public class BbtForumController {
 
  @Autowired
  private BbtForumService bbtForumService;
 
  @ModelAttribute("items")//<——①向模型對象中添加一個(gè)名為items的屬性
  public List<String> populateItems() {
    List<String> lists = new ArrayList<String>();
    lists.add("item1");
    lists.add("item2");
    return lists;
  }
 
  @RequestMapping(params = "method=listAllBoard")
  public String listAllBoard(@ModelAttribute("currUser")User user, ModelMap model) {
    bbtForumService.getAllBoard();
    //<——②在此訪問模型中的items屬性
    System.out.println("model.items:" + ((List<String>)model.get("items")).size());
    return "listBoard";
  }
}

 在 ① 處,通過使用 @ModelAttribute 注解,populateItem() 方法將在任何請求處理方法執(zhí)行前調(diào)用,Spring MVC 會(huì)將該方法返回值以“items”為名放入到隱含的模型對象屬性列表中。

所以在 ② 處,我們就可以通過 ModelMap 入?yún)⒃L問到 items 屬性,當(dāng)執(zhí)行 listAllBoard() 請求處理方法時(shí),② 處將在控制臺(tái)打印出“model.items:2”的信息。當(dāng)然我們也可以在請求的視圖中訪問到模型對象中的 items 屬性。

小結(jié)

Spring 2.5 對 Spring MVC 進(jìn)行了很大增強(qiáng),現(xiàn)在我們幾乎完全可以使用基于注解的 Spring MVC 完全替換掉原來基于接口 Spring MVC 程序。基于注解的 Spring MVC 比之于基于接口的 Spring MVC 擁有以下幾點(diǎn)好處:

  • 方便請求和控制器的映射;
  • 方便請求處理方法入?yún)⒔壎║RL參數(shù);
  • Controller 不必繼承任何接口,它僅是一個(gè)簡單的 POJO。

但是基于注解的 Spring MVC 并不完美,還存在優(yōu)化的空間,因?yàn)樵谀承┡渲蒙纤然?XML 的配置更繁瑣。比如對于處理多個(gè)請求的 Controller 來說,假設(shè)我們使用一個(gè) URL 參數(shù)指定調(diào)用的處理方法(如 xxx.do?method=listBoardTopic),當(dāng)使用注解時(shí),每個(gè)請求處理方法都必須使用 @RequestMapping() 注解指定對應(yīng)的 URL 參數(shù)(如 @RequestMapping(params = "method=listBoardTopic")),而在 XML 配置中我們僅需要配置一個(gè) ParameterMethodNameResolver 就可以了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/lufeng20/article/details/7598801

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色视频亚洲 | 国产香蕉97碰碰久久人人 | www一区 | 亚洲欧美综合在线观看 | 视频国产精品 | 好大~好爽~再进去一点 | 国产成人综合久久 | 亚州性夜夜射在线观看 | 日韩 国产 欧美 精品 在线 | x8x8在线观看免费 | 4444亚洲国产成人精品 | www.色姐姐| 18欧美同性videos可播放 | 亚洲人成网站在线观看90影院 | 黄 色 成 年人在线 幻女free性俄罗斯第一次摘花 | 非洲一级毛片又粗又长aaaa | kuaibo成人播放器 | 亚洲国产精品综合久久一线 | 免费a视频在线观看 | 美女在线看永久免费网址 | 三级网站午夜三级 | 456亚洲人成高清在线 | 国产精品久久久精品日日 | 涩涩国产精品福利在线观看 | 欧美高清一区 | 国内精品哆啪啪 | 国产精品久久久久久久福利院 | 亚洲精品免费在线 | 欧美成a人片免费看久久 | 俄罗斯freeⅹ性欧美 | 美女私人影院 | 青青青手机在线观看 | 欧美一级艳片视频免费观看 | 高h全肉动漫在线观看免费 高h辣h双处全是肉军婚 | 91麻豆精品国产片在线观看 | 国产在视频线精品视频 | 亚瑟天堂久久一区二区影院 | 国产美女在线一区二区三区 | 日韩一品在线播放视频一品免费 | 手机看片福利 | 国产日韩欧美不卡www |