SpringMVC入門
1、Spring MVC 背景介紹
Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,可以選擇是使用內置的 Spring Web 框架還是 Struts 這樣的 Web 框架。通過策略接口,Spring 框架是高度可配置的,而且包含多種視圖技術,例如 JavaServer Pages(JSP)技術、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的視圖,所以不會強迫您只使用 JSP 技術。Spring MVC 分離了控制器、模型對象、分派器以及處理程序對象的角色,這種分離讓它們更容易進行定制。
2、常見MVC框架比較
運行性能上:
Jsp+servlet>struts1>spring mvc>struts2+freemarker>>struts2,ognl,值棧。
開發效率上,基本正好相反。值得強調的是,spring mvc開發效率和struts2不相上下。
Struts2的性能低的原因是因為OGNL和值棧造成的。所以,如果你的系統并發量高,可以使用freemaker進行顯示,而不是采用OGNL和值棧。這樣,在性能上會有相當大得提高。
3、SpringMVC 核心原理
1. 用戶發送請求給服務器。url:user.do
2. 服務器收到請求。發現DispatchServlet可以處理。于是調用DispatchServlet。
3. DispatchServlet內部,通過HandleMapping檢查這個url有沒有對應的Controller。如果有,則調用Controller。
4. Controller開始執行。
5. Controller執行完畢后,如果返回字符串,則ViewResolver將字符串轉化成相應的視圖對象;如果返回ModelAndView對象,該對象本身就包含了視圖對象信息。
6. DispatchServlet將執視圖對象中的數據,輸出給服務器。
7. 服務器將數據輸出給客戶端。
4、基于spring 3.0項目開發實例
1、導入相關jar包
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVCDemo</display-name> <servlet> <servlet-name>web servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>web servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、配置spring-mvc.xml
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--告知Spring,我們啟用注解驅動--> <mvc:annotation-driven /> <!--配置要掃描注解的類的路徑--> <context:component-scan base-package="com.excel.controller"></context:component-scan> <!--配置獲取VIEW的方式 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> </beans>
4、編寫控制器
package com.excel.controller; 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; @Controller @RequestMapping("user") public class UserController { //RequestParam 當傳遞參數名與接收名稱不一致時指定 @RequestMapping("/login.do") public String login(String userName ){ System.out.println("User Login...."+userName); return "index"; } }