在上面的随笔里已经把搭建springmvc环境的基本需要的包都下下来了,拉下来就是写配置文件了。
下面左图是总的结构,右图是增加包
一、最开始当然是web.xml文件了,这是一个总的宏观配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 上下文参数(第一启动),指定jdbc配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/jdbc.xml</param-value>
</context-param> <!-- spring监听器(第二启动),监听springMvc环境 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- springMvc编码拦截器(第三启动),springMvc内置的编码拦截器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- springMvc前置总控制器(第四启动),在分发其它的控制器前都要经过这个总控制器 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/springmvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 网站的默认首页 -->
<welcome-file-list>
<welcome-file>begin.jsp</welcome-file>
</welcome-file-list>
</web-app>
二、然后是jdbc.xml文件了,在这里面配置了数据源和事务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 指定connector连接信息的属性文件的位置 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置数据源,包含connector的驱动名称,地址,用户名以及密码 ,除了c3p0其实还有dbcp这种方式,但是hibernate官方已经宣布不再支持dbcp了-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 数据源事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 -->
<context:component-scan base-package="com.springmvc">
<context:exclude-filter type="regex" expression="com.spring.web.*" />
</context:component-scan> </beans>
三、springmvc.xml配置文件,具体的配置了springmvc
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 启动时扫描所有的controller -->
<context:component-scan base-package="com.springmvc.web" /> <!-- 主要作用于@Controller,激活该模式,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的 -->
<mvc:annotation-driven /> <!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->
<mvc:resources location="/WEB-INF/resources/**" mapping="/resources" /> <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
以上是最基本的配置,接下来还要配置二中对应的属性文件jdbc.properties以及log4j.properties
四、jdbc.properties数据库连接属性文件
#-----------------------mysql-------------------------
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springjdbc
jdbc.username=root
jdbc.password=lc1234
五、log4j.properties日志属性文件
#-----------------------------log4j-------------------------------
log4j.rootLogger=DEBUG,console,R log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=INFO
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.Append=true
log4j.appender.R.File=D:/springmvc.log
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n
log4j.appender.R.MaxFileSize=20MB log4j.org.springframework.web=DEBUG