SpringMVC和spring集成zxw.docx
《SpringMVC和spring集成zxw.docx》由会员分享,可在线阅读,更多相关《SpringMVC和spring集成zxw.docx(12页珍藏版)》请在冰豆网上搜索。
![SpringMVC和spring集成zxw.docx](https://file1.bdocx.com/fileroot1/2022-10/29/4701e5d9-0994-44af-9dd2-558265f95e28/4701e5d9-0994-44af-9dd2-558265f95e281.gif)
SpringMVC和spring集成zxw
SpringMVC和spring集成
Author:
zxw
Time:
2015-05-15
1、SpringMVC项目:
新建一个配置文件的源文件目录:
2、导入SpringMVC所需要的jar包:
(http:
//www.springsource.org/spring-community-download)或者右键创建AddSpring:
此实例需要收到导入的jar包有(如果用到其它jar包可手动再次导入):
org.springframework.aop-3.0.5.RELEASE.jar:
与Aop编程相关的包
org.springframework.beans-3.0.5.RELEASE.jar:
提供了简捷操作bean的接口
org.springframework.context-3.0.5.RELEASE.jar:
构建在beans包基础上,用来处理资源文件及国际化。
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar:
spring核心包
org.springframework.web-3.0.5.RELEASE.jar:
web核心包,提供了web层接口
org.springframework.web.servlet-3.0.5.RELEASE.jar:
web层的一个具体实现包,DispatcherServlet也位于此包中。
org.springframework.asm-3.0.5.RELEASE.jar
mons.logging-1.1.1.jar
org.springframework.expression-3.0.5.RELEASE.jar
为了方便,建议在搭建环境中导入spring的所有jar包(所有jar包位于dist目录下),不需要的jar包可以在以后删除。
如果报错的话,可以导入下面jstl的两个核心jar包:
jstl.jarjstl标签的核心包。
standard.jar标准标签库包(此包也可以不导入)。
3、index.jsp首页内容:
第一步:
开始访问springMVC和spring集成
4、success.jsp首页内容:
成功页面!
5、web.xml内容:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
--加载顺序:
context-param>listener>filter>servlet-->
contextConfigLocation
classpath:
spring/spring-core.xml
org.springframework.web.context.ContextLoaderListener
--第三步:
加载解析初始化Spring配置文件(利用总分配器DispatcherServlet处理请求到Spring配置文件)-->
User
org.springframework.web.servlet.DispatcherServlet
--读取Spring配置文件,加载解析初始化(默认查找路径:
/WEB-INF/[servlet-name]-servlet.xml)-->
contextConfigLocation
--查找spring配置文件路径(不配置init-param的话,默认为/WEB-INF/下Login-servlet.xml)-->
classpath:
spring/User-servlet.xml
--classpath路径为:
\WebRoot\WEB-INF\classes-->
1
--tomcat启动时加载解析DispatcherServlet类(如果不设置启动,则第一次访问时启动,比较慢)-->
--第二步:
index页面请求映射-->
User
*.html
--配置编码格式过滤器-->
EncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
--初始化编码是UTF-8-->
forceEncoding
true
--强制转换response和request.setCharacterEncoding("UTF-8")编码格式-->
--过滤器映射-->
EncodingFilter
/*
--过滤所有请求-->
index.jsp
6、spring配置文件User-servlet.xml内容:
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
aop="http:
//www.springframework.org/schema/aop"xmlns:
context="http:
//www.springframework.org/schema/context"
xmlns:
p="http:
//www.springframework.org/schema/p"xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
mvc="http:
//www.springframework.org/schema/mvc"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd">
--第四步:
启动注解组件扫描加载指定包下面的注解(需包含头部的:
xmlns、xmlns:
context、xmlns:
mvc、xsi:
schemaLocation)-->
component-scanbase-package="com.test"/>
--第八步:
利用视图解析器继承扩展UrlBasedViewResolver的子类InternalResourceViewResolver解析返回的模型页面-->
--InternalResourceViewResolver解析器可以解析jstl所以不需要此viewClass-->
--此路径下文件安全性高,不可直接访问-->
--查找文件的后缀-->
7、spring-core.xml内容:
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEb