Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,能夠選擇是使用內置的 Spring Web 框架仍是 Struts 這樣的 Web 框架。經過策略接口,Spring 框架是高度可配置的,並且包含多種視圖技術,例如 JavaServer Pages(JSP)技術、Velocity、Tiles、iText 和 POI。Spring MVC 框架並不知道使用的視圖,因此不會強迫您只使用 JSP 技術。Spring MVC 分離了控制器、模型對象、分派器以及處理程序對象的角色,這種分離讓它們更容易進行定製。web
Spring 的 Web MVC 框架是圍繞 DispatcherServlet
設計的,它把請求分派給處理程序,同時帶有可配置的處理程序映射、視圖解析、本地語言、主題解析以及上載文件支持。默認的處理程序是很是簡單的 Controller
接口,只有一個方法 ModelAndView handleRequest(request, response)
。Spring 提供了一個控制器層次結構,能夠派生子類。若是應用程序須要處理用戶輸入表單,那麼能夠繼承 AbstractFormController
。若是須要把多頁輸入處理到一個表單,那麼能夠繼承 AbstractWizardFormController
。spring
示例應用程序有助於直觀地學習這些特性。銀行應用程序容許用戶檢索他們的賬戶信息。在構建銀行應用程序的過程當中,能夠學到如何配置 Spring MVC 框架和實現框架的視圖層,視圖層包括 JSTL 標記(用於顯示輸出的數據)和JavaServer Pages 技術。session
要開始構建示例應用程序,請配置 Spring MVC 的 DispatcherServlet
。請在 web.xml 文件中註冊全部配置。清單 1 顯示瞭如何配置 sampleBankingServlet
。架構
<servlet> <servlet-name>sampleBankingServlet</servlet-name> <servlet-class> org.springframework.we.servlet.DispatcherServlet <servlet-class> <load-on-startup>1<load-on-startup> <servlet>
DispatcherServlet
從一個 XML 文件裝入 Spring 應用程序上下文,XML 文件的名稱是 servlet 的名稱後面加上 -servlet 。在這個示例中,DispatcherServlet
會從 sampleBankingServlet-servlet.xml 文件裝入應用程序上下文。app
下一步是配置想讓 sampleBankingServlet
處理的 URL。一樣,仍是要在 web.xml 中註冊全部這些信息。框架
<servlet-mapping> <servlet-name> sampleBankingServlet<servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping>
下面,裝入配置文件。爲了作到這點,請爲 Servlet 2.3 規範註冊 ContextLoaderListener
或爲 Servlet 2.2 及如下的容器註冊 ContextLoaderServlet
。爲了保障後向兼容性,請用 ContextLoaderServlet
。在啓動 Web 應用程序時,ContextLoaderServlet
會裝入 Spring 配置文件。清單 3 註冊了 ContextLoaderServlet
。jsp
<servlet> <servlet-name>context>servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
contextConfigLocation
參數定義了要裝入的 Spring 配置文件,以下面的 servlet 上下文所示。學習
<context-param> <param-value>contextConfigLocation</param-value> <param-value>/WEB-INF/sampleBanking-services.xml</param-value> </context-param>
sampleBanking-services.xml 文件表明示例銀行應用程序服務的配置和 bean 配置。若是想裝入多個配置文件,能夠在 <param-value>
標記中用逗號做分隔符。url
示例銀行應用程序容許用戶根據唯一的 ID 和口令查看賬戶信息。雖然 Spring MVC 提供了其餘選項,可是我將採用 JSP 技術做爲視圖頁面。這個簡單的應用程序包含一個視圖頁用於用戶輸入(ID 和口令),另外一頁顯示用戶的賬戶信息。spa
我從 LoginBankController
開始,它擴展了 Spring MVC 的 SimpleFormController
。SimpleFormContoller
提供了顯示從 HTTP GET
請求接收到的表單的功能,以及處理從 HTTP POST
接收到的相同表單數據的功能。LoginBankController
用 AuthenticationService
和 AccountServices
服務進行驗證,並執行賬戶活動。「 配置視圖屬性 」一節中的 清單 5 描述瞭如何把 AuthenticationService
和 AccountServices
鏈接到 LoginBankController
。 清單 4 顯示了 LoginBankController
的代碼。
下面,我必須註冊在接收到 HTTP GET
請求時顯示的頁面。我在 Spring 配置中用 formView
屬性註冊這個頁面,如清單 5 所示。sucessView
屬性表明表單數據提交併且 doSubmitAction()
方法中的邏輯成功執行以後顯示的頁面。formView
和 sucessView
屬性都表明被定義的視圖的邏輯名稱,邏輯名稱映射到實際的視圖頁面。
<bean id="loginBankController" class="springexample.controller.LoginBankController"> <property name="sessionForm"><value>true</value></property> <property name="commandName"><value>loginCommand</value></property> <property name="commandClass"> <value>springexample.commands.LoginCommand</value> </property> <property name="authenticationService"> <ref bean="authenticationService" /> </property> <property name="accountServices"> <ref bean="accountServices" /> </property> <property name="formView"> <value>login</value> </property> <property name="successView"> <value>accountdetail</value> </property> </bean>
commandClass
和 commandName
標記決定將在視圖頁面中活動的 bean。例如,能夠經過 login.jsp 頁面訪問 loginCommand
bean,這個頁面是應用程序的登陸頁面。一旦用戶提交了登陸頁面,應用程序就能夠從 LoginBankController
的 onSubmit()
方法中的命令對象檢索出表單數據。
Spring MVC 的 視圖解析器 把每一個邏輯名稱解析成實際的資源,即包含賬戶信息的 JSP 文件。我用的是 Spring 的 InternalResourceViewResolver
,如 清單 6 所示。
由於我在 JSP 頁面中使用了 JSTL 標記,因此用戶的登陸名稱解析成資源 /jsp/login.jsp,而 viewClass
成爲 JstlView
。
就像前面提到的,LoginBankController
內部鏈接了 Spring 的 AccountServices
和 AuthenticationService
。AuthenticationService
類處理銀行應用程序的驗證。AccountServices
類處理典型的銀行服務,例如查找交易和電匯。清單 7 顯示了銀行應用程序的驗證和賬戶服務的配置。
<beans> <bean id="accountServices" class="springexample.services.AccountServices"> </bean> <bean id="authenticationService" class="springexample.services.AuthenticationService"> </bean> </beans>
以上服務在 sampleBanking-services.xml 中註冊,而後裝入 web.xml 文件中,就像 前面討論的那樣。控制器和服務配置好後,這個簡單的應用程序就完成了。