1、maven创建项目
2、 项目的基本目录是这样婶的:
3、spring注解方式搭建最简单项目框架
几个重要的配置文件:
web.xml
applicationContext.xml Spring的配置文件
jdbc.properties数据库配置文件
log4j.properties日志配置文件
dispatcher-servlet.xml spring MVC配置文件
下面给出几个配置文件的详细代码
web.xml:此文件装载spring、spring MVC、log4j及spring监听器、乱码处理
<?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">
<!-- Spring context startup Spring的初始化-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value><!-- Spring的配置文件 -->
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring character filter 处理乱码-->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Configuration of Log4j log4j的配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value><!-- log4j.properties文件路径 -->
classpath:log4j.properties
</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- Spring MVC Configuration Spring MVC的配置-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value><!-- dispatcher-servlet.xml文件路径 -->
classpath:dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
jdbc.properties