标签:
一、搭建Spring MVC框架步骤
1、创建Dynamic Web Project
2、导入spring和springmvc所需要的文件(jar包)
3、配置web.xml文件
<span style="font-size:18px;"> <span style="font-size:14px;"> <!-- Spring 配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext-*.xml</param-value> </context-param> <pre name="code" class="html"> <!-- spring contextConfigLocation 监听配置装载 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </span></span>
<span style="font-size:14px;"> <!-- Spring MVC --> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext-servlet.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <pre name="code" class="html"> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping></span>
<span style="font-size:14px;"> <!-- 首页文件名称 --> <welcome-file-list> <!-- <welcome-file>/view/default.jsp</welcome-file> --> <welcome-file>/view/default.jsp</welcome-file> </welcome-file-list> <!-- 404错误页面 --> <error-page> <error-code>404</error-code> <location>/home/exception.htm?errorCode=404</location> </error-page></span>3.1 监听spring上下文容器
3.2 加载spring的xml文件到spring的上下文容器(spring-context.xml)
3.3 配置spring MVC的DispatcherServlet
3.4 加载spring MVC的xml到spring的上下文容器(springMVC-context.xml)
3.5 配置DispatcherServlet所需要拦截的 url(固定了HTTP的格式 如*.do)
4、配置spring的xml文件----主要配置链接数据库等信息application-datasource.xml
<span style="font-size:14px;"><?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:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"> <!-- 载入属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 初始数据库连接,非实际 --> <bean id="initdataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc.url}"/> <property name="username" value="admin"/> <property name="password" value="123456"/> <property name="driverClassName" value="${jdbc.driverClassName}"/> </bean> <!-- 以下为其他开源数据库连接池配置 username和password一般要注释掉,使用密码加密--> <bean id="realDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="initialSize" value="8"/> <property name="maxActive" value="20"/> <property name="maxIdle" value="20"/> <property name="minIdle" value="5"/> <property name="maxWait" value="30000"/> <property name="removeAbandonedTimeout" value="20"/> <property name="removeAbandoned" value="true"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="validationQuery" value="select sysdate from dual"/> <property name="validationQueryTimeout" value="1"/> <property name="timeBetweenEvictionRunsMillis" value="30000"/> <property name="numTestsPerEvictionRun" value="20"/> <!-- <property name="username" value="${jdbc.username}"/> --> <!-- <property name="password" value="${jdbc.password}"/> --> </bean> <!-- 解密数据库密码 --> <bean id="egovaDataSource" class="cn.com.egova.config.EgovaDataSource"> <property name="initdataSource" ref="initdataSource"/> <property name="dataSource" ref="realDataSource"/> </bean> <!-- Oracle大对象处理 --> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true"/> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"> <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/> </bean> <!-- 通过aop配置事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- get*, load*, list*, find* 不启用回滚,只读事务保持多条sql数据一致--> <tx:method name="get*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceAllOperation" expression="execution(* cn.com.cd..dao.*Manager.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceAllOperation" order="2" /> </aop:config> <!-- 支持通过注解添加事务 --> <tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/> <!-- 解添方式添加缓存 --> <ehcache:annotation-driven cache-manager="ehCacheManager" /> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> </beans></span>
5、配置spring MVC的xml文件-----application-servlet.xml
5.1 加载spring的全局配置文件
5.2 扫描指定包下的所有类是注解生效
5.3 配置SpringMVC的视图渲染器
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- ①:扫描注解,解释为bean --> <context:component-scan base-package="cn.com.cd"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射, 完成json自动转换 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> <bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"/> <property name="suffix" value=".jsp"/> </bean> <!-- ④:定义文件上传处理器,最大5M --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> </bean> <!-- ⑤:Aop 代理设置 --> <aop:aspectj-autoproxy/> <!-- ⑥:JSR-303 support 暂时没有使用 --> <mvc:annotation-driven/> <mvc:resources mapping="/other/**" location="/other/"/> <mvc:resources mapping="/library/**" location="/library/"/> </beans>
6、写Controller(TestController.java)
7、写jsp文件
执行流程个人总结:接收到请求后会扫描springMVC配置文件中指定的包中的类(controller),根据controller中的注解@RequestMapping("test")找到对应的jsp文件。
二、运行流程
在整个 Spring MVC框架中,DispatcherServlet处于核心位置,负责协调和组织不同组件以完成请求处理并返回响应的工作
SpringMVC处理请求过程:
1). 若一个请求匹配DispatcherServlet的请求映射路径(在 web.xml 中指定), WEB容器将该请求转交给DispatcherServlet处理
2). DispatcherServlet接收到请求后, 将根据请求信息(包括 URL、HTTP方法、请求头、请求参数、Cookie 等)及HandlerMapping的配置找到处理请求的处理器(Handler).可将HandlerMapping看成路由控制器,将 Handler 看成目标主机。
3). 当DispatcherServlet根据HandlerMapping得到对应当前请求的 Handler后,通过HandlerAdapter对 Handler 进行封装,再以统一的适配器接口调用 Handler。
4). 处理器完成业务逻辑的处理后将返回一个ModelAndView给DispatcherServlet,ModelAndView包含了视图逻辑名和模型数据信息
5). DispatcherServlet借助ViewResoler完成逻辑视图名到真实视图对象的解析
6). 得到真实视图对象 View 后, DispatcherServlet使用这个 View对ModelAndView中的模型数据进行视图渲染。
标签:
原文地址:http://blog.csdn.net/sweetgirl520/article/details/51364075