版权声明:本文为博主原创文章,未经博主允许不得转载。
一、简介
使用SSM框架做了一些项目,一直都是使用别人已经搭建好的框架。于是自己尝试着搭建了一下框架,并研究了一下配置文件。
基本的组织结构和用法就不用多说了,各种官方文档上比较全面
二、环境与框架
开发环境MyEclipse+JDK1.7+Tomcat7
开发环境配置请查阅博客
本文按照开发环境搭建,完整的结构框架如下
三、配置文件详解
1.web.xml
web.xml应该是整个项目中最重要的配置文件了,配置方式与普通的servlet基本相似
<span style="font-size:14px;"> <servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping></span>
servlet 配置前端控制器,其中
ContextconfigLocation配置配置springmvc加载的配置文件适配器、处理映射等等
url-pattern中 .* :访问以.*结尾 由DispatcherServlet进行解析
/ :所有访问由DispatcherServlet进行解析
配置Spring容器:
<span style="font-size:14px;"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring-mvc.xml</param-value>
</context-param></span>
配置日志文件:
<span style="font-size:14px;"> <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param></span>
配置监听器:
<span style="font-size:14px;"> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener></span>
<span style="font-size:14px;"> <filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></span>
配置处理错误页面:
<span style="font-size:14px;"><error-page>
<span style="white-space:pre"> </span><exception-type>java.lang.Throwable</exception-type>
<span style="white-space:pre"> </span><location>/error.jsp</location>
</error-page>
<error-page>
<span style="white-space:pre"> </span><error-code>404</error-code>
<span s