web.xml文件配置
View Code
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>myMainWeb</display-name> <!-- --> <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> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>*.htm</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> <!-- 配置Sring MVC的核心控制器DispatcherServlet DispatcherServlet从下面的xml文件中装入Spring应用程序上下文 这里会从spring-front.xml和spring-persist及其spring-service.xml三个文件中加载应用程序上下文.并且所有.jsp的请求都会被转发 --> <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:/spring-front.xml, classpath:/spring-persist.xml, classpath:/spring-service.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- springMVC建立映射,所有.jsp的请求都会被转发到dispatcherservlet进行处理 --> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/index.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.htm</welcome-file> </welcome-file-list> </web-app>
其中用到的web.properties文件内容为
View Code
input.encoding=UTF-8 output.encoding=UTF-8 #whether load template from file system,when this option enabled,will support hot deployment of templates velocity.preferFileSystemAccess=true velocity.resourceLoaderPath=/WEB-INF/views/,/WEB-INF/views_sys velocity.suffix=.vm velocity.userdirective=com.zz91.util.velocity.CacheFragmentDirective memcached.server=192.168.110.119:11211 #########sso config########### sso.api=http://192.168.110.120:7017/zz91/api sso.domain=zz91.com log.host=http://192.168.110.120:7128/zz91-log log.appcode=zz91 mail.host=http://192.168.110.120:7128/zz91-mail tags.host=http://192.168.110.120:7128/tags/api #sms config sms.host=http://192.168.110.120:7128/zz91-sms/ search.host=192.168.110.120 search.port=9315 ## address toolbox## address.prop=file:/usr/tools/config/address/zz91.properties
web项目下面的spring-front.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="byName" default-lazy-init="true"> <context:component-scan base-package="com.myBussiness"/> <!-- 配置数据源, 配置参数从web.peroperties文件中读取--> <bean id="webPropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true" /> <!--property name="placeholderPrefix" value="${web_" / --> <property name="locations"> <list> <value>classpath:/web.properties</value> </list> </property> </bean> <!--springMVC最核心的配置,控制请求具体进入的controller --> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="caseSensitive" value="false" /> <property name="basePackage" value="com.myBussiness.front.controller" /> <!--<property name="defaultHandler" value="rootController" /> --></bean> <!-- 配置celocity的视图加载引擎,用于加载resourceLoaderPath下面的vm模版 --> <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="resourceLoaderPath" value="${velocity.resourceLoaderPath}" /> <property name="preferFileSystemAccess" value="${velocity.preferFileSystemAccess}" /> <property name="velocityProperties"> <props> <prop key="input.encoding">${input.encoding}</prop> <prop key="output.encoding">${output.encoding}</prop> <prop key="file.resource.loader.cache">false</prop> <prop key="file.resource.loader.modificationCheckInterval">2</prop> <!--<prop key="velocimacro.library">macro.vm</prop> <prop key="userdirective">${velocity.userdirective}</prop> --><!-- 针对开发而非实际应用的 --> <prop key="velocimacro.library.autoreload">true</prop> </props> </property> </bean> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="velocityEngine" ref="velocityEngine" /> </bean> <!-- 配置velocity的视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="exposeSpringMacroHelpers" value="true" /> <property name="suffix" value="${velocity.suffix}" /> <property name="layoutUrl" value="layout/default.vm" /> <property name="contentType" value="text/html;charset=utf-8" /> <!--<property name="toolboxConfigLocation" value="WEB-INF/vm-toolbox.xml" /> --></bean> </beans>
spring-service.xml
View Code
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="byName" default-lazy-init="true"> <context:component-scan base-package="cn.myBussiness.service" /> </beans>
spring-persist.xml文件
View Code
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="byName" default-lazy-init="true"> <context:component-scan base-package="cn.myBussiness.persist" /> <!--p配置数据源.告诉spring去那里着数据库的配置信息,变量从persist.properties中获取--> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <value>classpath:persist.properties</value> </list> </property> </bean> <!-- 配置数据源需要的驱动,用户名及其密码等等信息 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${jdbc.driver}</value> </property> <property name="jdbcUrl"> <value>${jdbc.astUrl}</value> </property> <property name="properties"> <props> <prop key="c3p0.minPoolSize">2</prop> <prop key="c3p0.maxPoolSize">50</prop> <prop key="c3p0.timeout">5000</prop> <prop key="c3p0.max_statement">100</prop> <prop key="c3p0.testConnectionOnCheckout">true</prop> <prop key="c3p0.idleConnectionTestPeriod">18000</prop> <prop key="automaticTestTable">mail_template</prop> <prop key="c3p0.maxIdleTime">25000</prop> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> </props> </property> </bean> <!--根据dataSource和configLocation创建一个SqlMapClient 这个映射文件映射每个模型实例 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations"> <list> <value>classpath:/SqlMapConfig.xml</value> </list> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> </beans>
其中数据库的配置信息在文件
persist.properties文件中
View Code
jdbc.driver=com.mysql.jdbc.Driver jdbc.astUrl=jdbc:mysql://192.168.2.10:3306/user?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull jdbc.username=root jdbc.password=zj88friend jdbc.maxPoolSize=50 jdbc.minPoolSize=10 jdbc.initialPoolSize=10 jdbc.maxIdleTime=30000 jdbc.acquireIncrement=10 sqlMap.root=com/ast/ast1949/ibatismaps