摘要:
- SpringMVC介紹
- 入門程序
- SpringMVC架構講解
- 框架結構
- 組件說明
- SpringMVC整合MyBatis
- 參數綁定
- SpringMVC默認支持的類型
- 簡單數據類型
- Pojo類型
- Pojo包裝類型
- 自定義參數綁定
- SpringMVC和Struts2的區別
2. Spring入門
2.1. Springmvc是什麼
Spring web mvc和Struts2都屬於表現層的框架,它是Spring框架的一部分,我們可以從Spring的整體結構中看得出來,如下圖:
2.2. Springmvc處理流程
2.3. 入門程序
需求:使用瀏覽器顯示商品列表
2.3.1. 創建web工程
2.3.2. 導入jar包
2.3.3. 加入配置文件
2.3.3.1. 創建springmvc.xml
創建SpringMVC的核心配置文件
SpringMVC本身就是Spring的子項目,對Spring兼容性很好,不需要做很多配置。
這裏只配置一個Controller掃描就可以了,讓Spring對頁面控制層Controller進行管理。
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置controller掃描包=================== -->
<context:component-scan base-package="com.itheima" />
<!-- 配置處理器映射器,默認的已經廢棄 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> -->
<!-- 配置處理器適配器,默認的已經廢棄 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> -->
<!-- 註解驅動,替代上面兩個====================== -->
<mvc:annotation-driven/>
<!-- 配置視圖解析器 ==========================-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置邏輯視圖的前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置邏輯視圖的後綴 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.3.3.2. 配置前端控制器
配置SpringMVC的前端控制器DispatcherServlet
在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMvc_day01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默認路徑是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1. /*攔截所有,全部攔截
2. *.action *.do 攔截以do,action結尾的請求 ERP
3. /攔截所有(不包括jsp)(包含 .js .png .css) 強烈建議使用 面向前端消費者 對靜態資源放行
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.3.4. 加入jsp頁面
把參考資料中的itemList.jsp複製到工程的/WEB-INF/jsp目錄下,如下圖:
2.3.5. 實現顯示商品列表頁
2.3.5.1. 創建pojo
分析頁面,查看頁面需要的數據,如下圖:
2.3.5.2. 創建ItemController
ItemController是一個普通的java類,不需要實現任何接口。
需要在類上添加@Controller註解,把Controller交由Spring管理
在方法上面添加@RequestMapping註解,裏面指定請求的url。其中“.action”可以加也可以不加。
3. Springmvc架構
3.1. 框架結構
框架結構如下圖:
3.2. 架構流程
- 用戶發送請求至前端控制器DispatcherServlet
- DispatcherServlet收到請求調用HandlerMapping處理器映射器。
- 處理器映射器根據請求url找到具體的處理器,生成處理器對象及處理器攔截器(如果有則生成)一併返回給DispatcherServlet。
- DispatcherServlet通過HandlerAdapter處理器適配器調用處理器
- 執行處理器(Controller,也叫後端控制器)。
- Controller執行完成返回ModelAndView
- HandlerAdapter將controller執行結果ModelAndView返回給DispatcherServlet
- DispatcherServlet將ModelAndView傳給ViewReslover視圖解析器
- ViewReslover解析後返回具體View
- DispatcherServlet對View進行渲染視圖(即將模型數據填充至視圖中)。
- DispatcherServlet響應用戶
3.3. 組件說明
以下組件通常使用框架提供實現:
- DispatcherServlet:前端控制器
用戶請求到達前端控制器,它就相當於mvc模式中的c,dispatcherServlet是整個流程控制的中心,由它調用其它組件處理用戶的請求,dispatcherServlet的存在降低了組件之間的耦合性。
- HandlerMapping:處理器映射器
HandlerMapping負責根據用戶請求url找到Handler即處理器,springmvc提供了不同的映射器實現不同的映射方式,例如:配置文件方式,實現接口方式,註解方式等。
- Handler:處理器
Handler 是繼DispatcherServlet前端控制器的後端控制器,在DispatcherServlet的控制下Handler對具體的用戶請求進行處理。
由於Handler涉及到具體的用戶業務請求,所以一般情況需要程序員根據業務需求開發Handler。
- HandlAdapter:處理器適配器
通過HandlerAdapter對處理器進行執行,這是適配器模式的應用,通過擴展適配器可以對更多類型的處理器進行執行。
下圖是許多不同的適配器,最終都可以使用usb接口連接
- ViewResolver:視圖解析器
View Resolver負責將處理結果生成View視圖,View Resolver首先根據邏輯視圖名解析成物理視圖名即具體的頁面地址,再生成View視圖對象,最後對View進行渲染將處理結果通過頁面展示給用戶。
- View:視圖
springmvc框架提供了很多的View視圖類型的支持,包括:jstlView、freemarkerView、pdfView等。我們最常用的視圖就是jsp。
一般情況下需要通過頁面標籤或頁面模版技術將模型數據通過頁面展示給用戶,需要由程序員根據業務需求開發具體的頁面。
說明:在springmvc的各個組件中,處理器映射器、處理器適配器、視圖解析器稱爲springmvc的三大組件。
需要用戶開發的組件有handler、view
3.4. 默認加載的組件
我們沒有做任何配置,就可以使用這些組件
因爲框架已經默認加載這些組件了,配置文件位置如下圖:
3.5. 組件掃描器
使用組件掃描器省去在spring容器配置每個Controller類的繁瑣。
使用<context:component-scan>自動掃描標記@Controller的控制器類,
在springmvc.xml配置文件中配置如下:
3.6. 註解映射器和適配器
3.6.1. 配置處理器映射器
註解式處理器映射器,對類中標記了@ResquestMapping的方法進行映射。根據@ResquestMapping定義的url匹配@ResquestMapping標記的方法,匹配成功返回HandlerMethod對象給前端控制器。
HandlerMethod對象中封裝url對應的方法Method。
從spring3.1版本開始,廢除了DefaultAnnotationHandlerMapping的使用,推薦使用RequestMappingHandlerMapping完成註解式處理器映射。
在springmvc.xml配置文件中配置如下:
註解描述:
@RequestMapping:定義請求url到處理器功能方法的映射
3.6.2. 配置處理器適配器
註解式處理器適配器,對標記@ResquestMapping的方法進行適配。
從spring3.1版本開始,廢除了AnnotationMethodHandlerAdapter的使用,推薦使用RequestMappingHandlerAdapter完成註解式處理器適配。
在springmvc.xml配置文件中配置如下:
3.6.3. 註解驅動
直接配置處理器映射器和處理器適配器比較麻煩,可以使用註解驅動來加載。
SpringMVC使用<mvc:annotation-driven>自動加載RequestMappingHandlerMapping和RequestMappingHandlerAdapter
可以在springmvc.xml配置文件中使用<mvc:annotation-driven>替代註解處理器和適配器的配置。
3.7. 視圖解析器
視圖解析器使用SpringMVC框架默認的InternalResourceViewResolver,這個視圖解析器支持JSP視圖解析
在springmvc.xml配置文件中配置如下:
邏輯視圖名需要在controller中返回ModelAndView指定,比如邏輯視圖名爲ItemList,則最終返回的jsp視圖地址:
“WEB-INF/jsp/itemList.jsp”
最終jsp物理地址:前綴+邏輯視圖名+後綴
4. 整合mybatis
爲了更好的學習 springmvc和mybatis整合開發的方法,需要將springmvc和mybatis進行整合。
整合目標:控制層採用springmvc、持久層使用mybatis實現。
4.1. 創建數據庫表
sql腳本,位置如下圖:
4.2. 需要的jar包
1. spring(包括springmvc)
2. mybatis
3. mybatis-spring整合包
4. 數據庫驅動
5. 第三方連接池。
jar包位置如下圖:
4.3. 整合思路
Dao層:
- SqlMapConfig.xml,空文件即可,但是需要文件頭。
- applicationContext-dao.xml
- 數據庫連接池
- SqlSessionFactory對象,需要spring和mybatis整合包下的。
- 配置mapper文件掃描器。
Service層:
- applicationContext-service.xml包掃描器,掃描@service註解的類。
- applicationContext-trans.xml配置事務。
Controller層:
- Springmvc.xml
- 包掃描器,掃描@Controller註解的類。
- 配置註解驅動
- 配置視圖解析器
Web.xml文件:
- 配置spring
- 配置前端控制器。
4.4. 創建工程
4.5. 加入jar包
複製jar包到/WEB-INF/lib中
工程自動加載jar包
4.6. 加入配置文件
創建資源文件夾config
在其下創建mybatis和spring文件夾,用來存放配置文件,如下圖:
4.6.1. sqlMapConfig.xml
使用逆向工程來生成Mapper相關代碼,不需要配置別名。
在config/mybatis下創建SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 設置別名 -->
<typeAliases>
<!-- 2. 指定掃描包,會把包內所有的類都設置別名,別名的名稱就是類名,大小寫不敏感 -->
<package name="com.itheima.springmvc.pojo" />
</typeAliases>
</configuration>
4.6.2. applicationContext-dao.xml
配置數據源、配置SqlSessionFactory、mapper掃描器。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加載配置文件 ===================-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 數據庫連接池 ====================-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置SqlSessionFactory ==================-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 配置數據源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!--Mapper動態代理開發 掃描 ======================-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 導入基本包
1.不用傳入sqlSessionFactory,底層已經集成了
2.只需要傳入基本包,底層根據基本包把所有的 mapper的實現類都創建了,只需要根據接口名調用即可
-->
<property name="basePackage" value="com.itheima.springmvc.dao"/>
</bean>
<!-- 配置事務管理器=========================-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 註解式事務=============================== -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.6.3. db.properties
4.6.3. springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置controller掃描包 -->
<context:component-scan base-package="com.itheima" />
<!-- 配置處理器映射器,默認的已經廢棄 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> -->
<!-- 配置處理器適配器,默認的已經廢棄 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> -->
<!-- 註解驅動,替代上面兩個 -->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
<!-- 設置Convert轉換器 轉換日期、格式、去掉空格。。。 -->
<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 配置多個轉換器 -->
<property name="converters">
<list>
<bean class="com.itheima.springmvc.convert.DateConveter"/>
</list>
</property>
</bean>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4.6.7. 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC-Mybatis</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加載spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置解決post亂碼的過濾器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默認路徑是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1. /*攔截所有,全部攔截
2. *.action *.do 攔截以do,action結尾的請求 ERP
3. /攔截所有(不包括jsp)(包含 .js .png .css) 強烈建議使用 面向前端消費者 對靜態資源放行
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
4.7. 加入jsp頁面
4.8. 效果
5. 實現商品列表顯示
5.1. 需求
實現商品查詢列表,從mysql數據庫查詢商品信息。
5.2. DAO開發
使用逆向工程,生成代碼
5.3. ItemService接口
5.4. ItemServiceImpl實現類
5.5. ItemController
6. 參數綁定
6.1. 默認支持的參數類型
6.1.1. 需求
打開商品編輯頁面,展示商品信息。
6.1.2. 需求分析
編輯商品信息,首先要顯示商品詳情
需要根據商品id查詢商品信息,然後展示到頁面。
請求的url:/itemEdit.action
參數:id(商品id)
響應結果:商品編輯頁面,展示商品詳細信息。
6.1.3. ItemService接口
編寫ItemService接口如下圖:
6.1.4. ItemServiceImpl實現類
6.1.5. ItemController
頁面點擊修改按鈕,發起請求
http://127.0.0.1:8080/springmvc-web/itemEdit.action?id=1
需要從請求的參數中把請求的id取出來。
Id包含在Request對象中。可以從Request對象中取id。
想獲得Request對象只需要在Controller方法的形參中添加一個參數即可。Springmvc框架會自動把Request對象傳遞給方法。
代碼實現
6.1.6. 默認支持的參數類型
處理器形參中添加如下類型的參數處理適配器會默認識別並進行賦值。
6.1.6.1. HttpServletRequest
通過request對象獲取請求信息
6.1.6.2. HttpServletResponse
通過response處理響應信息
6.1.6.3. HttpSession
通過session對象得到session中存放的對象
6.1.7. Model/ModelMap
6.1.7.1. Model
除了ModelAndView以外,還可以使用Model來向頁面傳遞數據,
Model是一個接口,在參數裏直接聲明model即可。
如果使用Model則可以不使用ModelAndView對象,Model對象可以向頁面傳遞數據,View對象則可以使用String返回值替代。
不管是Model還是ModelAndView,其本質都是使用Request對象向jsp傳遞數據。
代碼實現:
6.1.7.2. ModelMap
ModelMap是Model接口的實現類,也可以通過ModelMap向頁面傳遞數據
使用Model和ModelMap的效果一樣,如果直接使用Model,springmvc會實例化ModelMap。
代碼實現:
6.2. 綁定簡單類型
當請求的參數名稱和處理器形參名稱一致時會將請求參數與形參進行綁定。
這樣,從Request取參數的方法就可以進一步簡化。
6.2.1. 支持的數據類型
參數類型推薦使用包裝數據類型,因爲基礎數據類型不可以爲null
整形:Integer、int
字符串:String
單精度:Float、float
雙精度:Double、double
布爾型:Boolean、boolean
說明:對於布爾類型的參數,請求的參數值爲true或false。或者1或0
請求url:
http://localhost:8080/xxx.action?id=2&status=false
處理器方法:
public String editItem(Model model,Integer id,Boolean status)
6.2.2. @RequestParam
使用@RequestParam常用於處理簡單類型的綁定。
value:參數名字,即入參的請求參數名字,如value=“itemId”表示請求的參數 區中的名字爲itemId的參數的值將傳入
required:是否必須,默認是true,表示請求中一定要有相應的參數,否則將報錯
TTP Status 400 - Required Integer parameter 'XXXX' is not present
defaultValue:默認值,表示如果請求中沒有同名參數時的默認值
定義如下:
6.3. 綁定pojo類型
6.3.1. 需求
將頁面修改後的商品信息保存到數據庫中。
6.3.2. 需求分析
請求的url:/updateItem.action
參數:表單中的數據。
響應內容:更新成功頁面
6.3.3. 使用pojo接收表單數據
如果提交的參數很多,或者提交的表單中的內容很多的時候,可以使用簡單類型接受數據,也可以使用pojo接收數據。
要求:pojo對象中的屬性名和表單中input的name屬性一致。
頁面定義如下圖:
Pojo(逆向工程生成)如下圖:
請求的參數名稱和pojo的屬性名稱一致,會自動將請求參數賦值給pojo的屬性。
6.3.6. ItemController
注意:
提交的表單中不要有日期類型的數據,否則會報400錯誤。如果想提交日期類型的數據需要用到後面的自定義參數綁定的內容。
6.3.8. 解決post亂碼問題
提交發現,保存成功,但是保存的是亂碼
在web.xml中加入:
以上可以解決post請求亂碼問題。
對於get請求中文參數出現亂碼解決方法有兩個:
修改tomcat配置文件添加編碼與工程編碼一致,如下:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
另外一種方法對參數進行重新編碼:
String userName new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
ISO8859-1是tomcat默認編碼,需要將tomcat編碼後的內容按utf-8編碼
6.4. 綁定包裝pojo
6.4.1. 需求
使用包裝的pojo接收商品信息的查詢條件。
6.4.2. 需求分析
包裝對象定義如下:
頁面定義如下圖:
6.4.3. 接收查詢條件
6.5. 自定義參數綁定
6.5.1. 需求
在商品修改頁面可以修改商品的生產日期,並且根據業務需求自定義日期格式。
6.5.2. 需求分析
由於日期數據有很多種格式,springmvc沒辦法把字符串轉換成日期類型。所以需要自定義參數綁定。
前端控制器接收到請求後,找到註解形式的處理器適配器,對RequestMapping標記的方法進行適配,並對方法中的形參進行參數綁定。可以在springmvc處理器適配器上自定義轉換器Converter進行參數綁定。
一般使用<mvc:annotation-driven/>註解驅動加載處理器適配器,可以在此標籤上進行配置。
6.5.3. 修改jsp頁面
如下圖修改itemEdit.jsp頁面,顯示時間
6.5.4. 自定義Converter
6.5.5. 配置Converter
我們同時可以配置多個的轉換器。在springmvc.xml裏面
7. springmvc與struts2不同
- springmvc的入口是一個servlet即前端控制器,而struts2入口是一個filter過濾器。
- springmvc是基於方法開發(一個url對應一個方法),請求參數傳遞到方法的形參,可以設計爲單例或多例(建議單例),struts2是基於類開發,傳遞參數是通過類的屬性,只能設計爲多例。
- Struts採用值棧存儲請求和響應的數據,通過OGNL存取數據, springmvc通過參數解析器是將request請求內容解析,並給方法形參賦值,將數據和視圖封裝成ModelAndView對象,最後又將ModelAndView中的模型數據通過request域傳輸到頁面。Jsp視圖解析器默認使用jstl。