Spring MVC项目配置

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
		http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	<display-name>WebProjectName</display-name>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>

	</welcome-file-list>
	<!-- 启动Spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Spring MVC的主控Servlet -->
	<servlet>
		<servlet-name>WebProjectName</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>加载spring-mvc的xml配置文件作为Spring MVC的配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<!-- 字符集编码过滤器 -->
	<filter>
		<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<servlet-mapping>
		<servlet-name>WebProjectName</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>
applicationContext.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/util
		http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/aop/
		http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
	
	<!-- 扫描类包,将标注Spring注解的类自动转化为Bean,同时自动完成Bean的注入 -->
	<context:component-scan base-package="com.bookstore.dao"></context:component-scan>
	<!-- 作用同上 -->
	<context:component-scan base-package="com.bookstore.service"></context:component-scan>
	<context:component-scan base-package="com.bookstore.control"></context:component-scan>
	
	<!-- 加载数据库连接信息properties文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据库连接信息 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		p:driverClassName="${driver}"
		p:url="${url}"
		p:username="${username}"
		p:password="${password}" />
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
		p:dataSource-ref="dataSource" />
		
		
	<!-- 配置事物管理器 -->
	<!-- <tx:annotation-driven /> -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
		p:dataSource-ref="dataSource"></bean>
	<!-- 配置视图解析器,将ModelAndView及其字符串解析到具体的页面 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView"
		p:prefix="/WEB-INF/WEB-VIEW/"
		p:suffix=".jsp"></bean>
		
	<!-- 配置访问静态文件 -->
	<!-- 
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
	<mvc:default-servlet-handler />
	 -->
        <!-- 路径请求映射,配置静态资源访问 -->
	<mvc:annotation-driven />
	<mvc:resources mapping="/**" location="/WEB-INF/WEB-VIEW/"/>
	
</beans>


其中数据库连接配置文件jdbc.properties内容如下:

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/mydatabase
username=root
password=123456




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值