spring mvc3.0 配置

糗事百科我用的是spring mvc的框架,那就先记录下spring mvc的搭建吧。


首先准备jar包,因为我还用了其他的东西,所以jar比较多,如果你只想用spring mvc的话,那么你只有百度一下具体的jar包了。下面是jar包下载的链接。


jar包



jar包配置好以后,

1、下面来配置一下web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
	<display-name></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>

	<!-- 初始化 Spring -->
	<listener>
		<display-name>Spring</display-name>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<description>Spring配置文件</description>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application.xml</param-value>
	</context-param>

	<!-- 初始化 Spring MVC -->
	<servlet>
		<servlet-name>Spring MVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>Spring MVC配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Spring MVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 设置session的有效时常 -->
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	
	
</web-app>


<url-pattern>/</url-pattern>中的/代表拦截所有的请求,你也可以写*.do表示拦截所有的以.do结尾的请求,这个可以根据需要自行百度

2、配置application.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-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
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<context:component-scan base-package="cn.qsbk.service"/>
	<context:component-scan base-package="cn.qsbk.dao"/>

	<!-- 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driverClass}" />
		<property name="jdbcUrl" value="${jdbcUrl}" />
		<property name="user" value="${user}" />
		<property name="password" value="${password}" />
		<property name="maxPoolSize" value="${maxPoolSize}" />
		<property name="minPoolSize" value="${minPoolSize}" />
		<property name="initialPoolSize" value="${initialPoolSize}" />
		<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
		<property name="maxIdleTime" value="${maxIdleTime}" />
	</bean>
	<!-- 加载jdbc资源文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 开启事务 -->
	<tx:annotation-driven transaction-manager="txManager"/>

    
      <!-- 配置Dao层 -->
    <bean id="commonDao" class="cn.qsbk.dao.common.ICommonDaoImpl">
    	<property name="dataSource" ref="dataSource"/>
    </bean>
 	
</beans>

上面文件中有一个加载数据源的资源文件,下面贴代码,你可以直接写在application.xml中,把所有的类似 ${driverClass}的代码直接写成以前jdbc的格式就行。



jdbc.properties

# the configuration of mysql database connection
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\://localhost\:3306/qsbk?characterEncoding\=UTF-8
user=root
password=123456
maxPoolSize=15
minPoolSize=5
initialPoolSize=10
idleConnectionTestPeriod = 18000
maxIdleTime=27800



3.配置spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.0.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-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
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- 视图控制 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> -->
		<property name="prefix" value="/view/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	

	<!-- 扫描控制器 -->
	<context:component-scan base-package="cn.qsbk.controller"/>
	
	<!-- 配置静态资源 -->
	<mvc:default-servlet-handler/>
	
	<!-- 配置Spring MVC注解驱动 -->
	<mvc:annotation-driven/>
	
	
</beans>


到此为止配置就算完成了,如果有问题欢迎讨论。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值