SpringMvc環境搭建(配置文件)css
在上面的隨筆裏已經把搭建springmvc環境的基本須要的包都下下來了,拉下來就是寫配置文件了。java
下面左圖是總的結構,右圖是增長包mysql
1、最開始固然是web.xml文件了,這是一個總的宏觀配置web
複製代碼spring
<?xml version="1.0" encoding="UTF-8"?>sql
<web-app version="2.5"數據庫
xmlns="http://java.sun.com/xml/ns/javaee"express
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"apache
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeespring-mvc
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>
複製代碼
2、而後是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>
複製代碼
3、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
4、jdbc.properties數據庫鏈接屬性文件
#-----------------------mysql-------------------------
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springjdbc
jdbc.username=root
jdbc.password=lc1234
5、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
複製代碼