初识Spring+SpringMVC+MyBatis框架(一)---web.xml
来源:互联网 发布:淘宝同城交易发货快吗 编辑:程序博客网 时间:2024/06/05 15:37
最近听说SSM框架,就想来研究研究,就参考http://www.baikeyang.com/code/96610.html网址代码重现了一遍,这个网址下面有源码下载地址,重现过程比较顺利。例子实现的是在该框架下实现增删改查,下面依次看看配置文件,代码,页面是如何编写的。
一、配置文件
配置文件主要包括web.xml、spring.xml、spring-mvc.xml、spring-mybatis.xml。
启动一个web项目,web容器首先读取web.xml,所以,先看看web.xml是如何配置的。
</pre><pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>My MVC</display-name> <context-param> <param-name>contextConfigLocation</param-name><param-value>classpath*:config/**/*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Encoding Filter --><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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC Dispatcher Servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
读取web.xml时,首先读取<listener>和<context-param>结点,web容器创建一个ServletContext (Servlet上下文),将context-param转化为键值对,交给ServletContext,然后,创建listener实例,创建监听器。在web.xml 中文件的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet。
在上面的web.xml的配置文件中,ContextLoaderListener监听器,加载类下面config文件夹下的各种配置文件,本项目中需要加载spring.xml、spring-mvc.xml、spring-mybatis.xml3个配置文件。
再往下定义了一个定义字符编码的过滤器filter,在该filter中,encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。
最后定义了一个Sevlet,DispatcherServlet是Spring MVC 核心分发器,是配置SpringMVC的第一步,用于拦截页面的请求,并将请求转发到相应的控制器Controller中去处理。<init-param>标签里配置contextConfigLocation初始化上下文,供DispatcherServlet在初始化时加载位于类路径下的config/spring-mvc.xml的配置文件作为SpringMVC的核心配置。如果不在这里配置contextConfigLocation,会默认加载“/WEB-INF/[servlet名字]-servlet.xml”文件。
注:<context-param>和<init-param>的区别
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/**/*.xml</param-value> </context-param>
在servlet中可得到参数
ServletContext context=getServletContext();System.out.println("1>>"+context.getInitParameter("contextConfigLocation"));<span style="color:#3d3d3d;"><span style="font-size:14px;"> </span></span>
(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
在程序中用this.getInitParameter("contextConfigLocation"));得到。
下一篇接着看Spring-mvc.xml和Spring.xml的配置。
- 初识Spring+SpringMVC+MyBatis框架(一)---web.xml
- 初识Spring+SpringMVC+MyBatis框架(三)---spring-mybatis.xml,AddMapping.xml
- 初识Spring+SpringMVC+MyBatis框架(二)---spring.xml和spring-mvc.xml
- 常用框架(一):spring+springMvc+mybatis+maven
- 搭建Spring SpringMVC Mybatis 框架(一)
- 常用框架(一):spring+springMvc+mybatis+maven
- 常用框架(一):spring+springMvc+mybatis+maven
- Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(一)
- spring+springMVC+mybatis框架
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- SSM框架(spring+springMVC+Mybatis) pom.xml文件
- SSM框架(spring+springMVC+Mybatis) pom.xml文件
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能
- spring+springmvc+druid+oscache+mybatis整合web.xml 配置内容
- SpringMVC、Spring和Mybatis整合之web.xml
- SSM框架Web程序的流程(Spring SpringMVC Mybatis)
- SSM框架Web程序的流程(Spring SpringMVC Mybatis)
- SSM框架Web程序的流程(Spring SpringMVC Mybatis)
- “mysql服务无法启动 发生系统错误1067”的解决办法
- 字符数组排序
- socket网络编程的一些基本函数
- 在使用Scripting.FileSystemObject 的时候报错automation服务器不能创建对象
- the android sdk folder can no longer be inside the application folder
- 初识Spring+SpringMVC+MyBatis框架(一)---web.xml
- [美文欣赏] 春风沉醉的晚上
- TeeChart在VC中实时曲线绘制的应用(工控),附实时曲线代码
- unicode编码相关资料
- automake编写完整的项目---动态库+可执行文件
- /drivers/net/phy/phy.c的状态机phy_state_machine分析
- 黑马程序员--------java基础知识 函数、数组
- 算法导论--学习笔记01
- 中国用人做事模式