首页 > 代码库 > SpringMVC配置环境
SpringMVC配置环境
一,lib目录下加入spring一般所需的jar包
二,配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>songtian</display-name>
<!-- Spring 全局上下文的 监听器,必须有spring上下文配置文件,否则报错 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring上下文配置, 默认为classpath:applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<!-- 字符集编码过滤器characterEncoding -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring mvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet上下文配置, 默认为/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>pages/sys/login/pages/signin.html</welcome-file>
</welcome-file-list>
</web-app>
三(1),配置spring-mvc.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描controller --> <context:component-scan base-package="com.st"/> <!-- 注解驱动:配置映射器和适配器驱动 --> <mvc:annotation-driven> <mvc:message-converters> <!-- 用fastjson替换默认的jackjson序列化方案 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
三(2),配置spring-config.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- jdbc.properteis 以下的 三(3) 指定数据源路径--> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- DataSource 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="http://www.mamicode.com/${mysql.driver}"/> <property name="url" value="http://www.mamicode.com/${mysql.url}"/> <property name="username" value="http://www.mamicode.com/${mysql.user}"/> <property name="password" value="http://www.mamicode.com/${mysql.password}"/> <property name="initialSize" value="http://www.mamicode.com/${initialSize}" /> <property name="maxActive" value="http://www.mamicode.com/${maxActive}" /> <property name="minIdle" value="http://www.mamicode.com/${minIdle}" /> <property name="maxWait" value="http://www.mamicode.com/${maxWait}" /> <property name="testOnBorrow" value="http://www.mamicode.com/true" /> <property name="testOnReturn" value="http://www.mamicode.com/true" /> <property name="testWhileIdle" value="http://www.mamicode.com/true" /> <property name="removeAbandoned" value="http://www.mamicode.com/true" /> <property name="logAbandoned" value="http://www.mamicode.com/true" /> <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/60000" /> <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/300000" /> <property name="removeAbandonedTimeout" value="http://www.mamicode.com/1800" /> <property name="validationQuery" value="http://www.mamicode.com/select 1 from dual" /> </bean> <!-- 配置事务开始 --> <bean id="proxyDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> <constructor-arg> <ref bean="dataSource" /> </constructor-arg> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="proxyDataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置事务结束 --> <!-- 使用构造方法加载数据源 --> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <!-- <constructor-arg index="0" ref="proxyDataSource"></constructor-arg> --> <constructor-arg> <ref bean="proxyDataSource"/> </constructor-arg> </bean> <!-- 发送邮件配置开始 --> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="http://www.mamicode.com/smtp.163.com"/> <property name="port" value="http://www.mamicode.com/25"/> <property name="username" value="http://www.mamicode.com/163邮箱"/> <property name="password" value="http://www.mamicode.com/邮箱授权码"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> </props> </property> </bean> <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="http://www.mamicode.com/163邮箱"/> <property name="subject" value="http://www.mamicode.com/发送邮件验证码" /> </bean> <!-- 发送邮件结束 --> <!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为5MB --> <property name="maxUploadSize"> <value>5242880</value> </property> </bean> </beans>
三(3)数据库连接配置 jdbc.properties
#mysql mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8 mysql.user=root mysql.password= initialSize=5 maxActive=100 minIdle=10 maxWait=6000
SpringMVC配置环境