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
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < 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" > |
03 | < display-name >SpringMVCDemo</ display-name > |
07 | < servlet-name >web servlet</ servlet-name > |
08 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
10 | < param-name >contextConfigLocation</ param-name > |
11 | < param-value >WEB-INF/spring-mvc.xml</ param-value > |
13 | < load-on-startup >1</ load-on-startup > |
17 | < servlet-name >web servlet</ servlet-name > |
18 | < url-pattern >/</ url-pattern > |
3、配置spring-mvc.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "http://www.springframework.org/schema/beans" |
03 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc = "http://www.springframework.org/schema/mvc" |
04 | xmlns:p = "http://www.springframework.org/schema/p" xmlns:context = "http://www.springframework.org/schema/context" |
05 | xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" |
06 | xsi:schemaLocation="http://www.springframework.org/schema/beans |
07 | http://www.springframework.org/schema/beans/spring-beans.xsd |
08 | http://www.springframework.org/schema/context |
09 | http://www.springframework.org/schema/context/spring-context.xsd |
10 | http://www.springframework.org/schema/aop |
11 | http://www.springframework.org/schema/aop/spring-aop.xsd |
12 | http://www.springframework.org/schema/tx |
13 | http://www.springframework.org/schema/tx/spring-tx.xsd |
14 | http://www.springframework.org/schema/mvc |
15 | http://www.springframework.org/schema/mvc/spring-mvc.xsd |
16 | http://www.springframework.org/schema/context |
17 | http://www.springframework.org/schema/context/spring-context.xsd"> |
20 | < mvc:annotation-driven /> |
23 | < context:component-scan base-package = "com.excel.controller" ></ context:component-scan > |
26 | < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" |
27 | p:prefix = "/WEB-INF/jsp/" p:suffix = ".jsp" /> |
4、编写控制器
01 | package com.excel.controller; |
03 | import org.springframework.stereotype.Controller; |
04 | import org.springframework.ui.ModelMap; |
05 | import org.springframework.web.bind.annotation.ModelAttribute; |
06 | import org.springframework.web.bind.annotation.RequestMapping; |
07 | import org.springframework.web.bind.annotation.RequestParam; |
08 | import org.springframework.web.bind.annotation.SessionAttributes; |
11 | @RequestMapping ( "user" ) |
12 | public class UserController { |
16 | @RequestMapping ( "/login.do" ) |
17 | public String login(String userName ){ |
18 | System.out.println( "User Login...." +userName); |