最近听说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的配置。
879

被折叠的 条评论
为什么被折叠?



