Spring MVC配置
完整的项目路径:百度云盘(博客园/project/testWeb.rar)
文件目录:
springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 <!-- 扫描controller包--> 13 <context:component-scan base-package="com.ssm.controller"></context:component-scan> 14 <!--适配器和映射器配置--> 15 <mvc:annotation-driven></mvc:annotation-driven> 16 <!--静态文件访问配置--> 17 <mvc:resources mapping="/static/**" location="/static/"></mvc:resources> 18 <!--视图解析器配置--> 19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> 20 </beans>
application-dao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <context:property-placeholder location="classpath:dbjdbc.properties"/> 14 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 15 <property name="driverClassName" value="${jdbc.driverClassName}" /> 16 <property name="url" value="${jdbc.url}" /> 17 <property name="username" value="${jdbc.username}" /> 18 <property name="password" value="${jdbc.password}" /> 19 </bean> 20 21 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 22 <property name="configLocation" value="classpath:mybatis/mybatis.xml"/> 23 <property name="dataSource" ref="dataSource"/> 24 </bean> 25 26 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 27 <property name="basePackage" value="com.ssm.mapper"/> 28 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 29 </bean> 30 31 </beans>
application-service.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 14 <bean id="weatherService" class="com.ssm.service.impl.WeatherServiceImpl"></bean> 15 <bean id="empService" class="com.ssm.service.impl.EmpServiceImpl"></bean> 16 17 </beans>
application-transaction.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:aop="http://www.springframework.org/schema/aop" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx.xsd 16 http://www.springframework.org/schema/aop 17 http://www.springframework.org/schema/aop/spring-aop.xsd"> 18 19 <!-- 配置事务 --> 20 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 21 <property name="dataSource" ref="dataSource"/> 22 </bean> 23 <tx:advice id="txAdvice" transaction-manager="txManager"> 24 <tx:attributes> 25 <tx:method name="add*" propagation="REQUIRED"/> 26 </tx:attributes> 27 </tx:advice> 28 <aop:config> 29 <aop:pointcut id="fooServiceOperation" expression="execution(* com.ssm.service.*.*(..))"/> 30 <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/> 31 </aop:config> 32 33 </beans>
mybatis.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 <!-- 5 <typeAliases> 6 <typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/> 7 </typeAliases> 8 <mappers> 9 <mapper resource="com/ssm/mapper/WeatherMapper.xml" /> 10 </mappers> 11 --> 12 </configuration>
dbjdbc.properties
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/test 3 jdbc.username=root 4 jdbc.password=123456
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>testWeb</display-name> 4 5 6 <!-- 上下文参数配置 --> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:/spring/application-*.xml</param-value> 10 </context-param> 11 12 <!-- spring监听器的配置 --> 13 <listener> 14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 15 </listener> 16 17 <!-- 前端控制器 --> 18 <servlet> 19 <servlet-name>springmvc</servlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21 <init-param> 22 <param-name>contextConfigLocation</param-name> 23 <param-value>classpath:spring/springmvc.xml</param-value> 24 </init-param> 25 </servlet> 26 <servlet-mapping> 27 <servlet-name>springmvc</servlet-name> 28 <url-pattern>/</url-pattern> 29 </servlet-mapping> 30 31 <welcome-file-list> 32 <welcome-file>index.jsp</welcome-file> 33 </welcome-file-list> 34 </web-app>
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>天气首页</title> 8 <link type="text/css" href="../static/testSty.css" rel="stylesheet"/> 9 </head> 10 <body> 11 <span class="testStyCss">天气界面</span> 12 <div class="divCss">hehe</div> 13 </body> 14 </html>
testSty.css
1 @CHARSET "UTF-8"; 2 3 .testStyCss{ 4 font-size: 16px; 5 color: red; 6 } 7 .divCss{ 8 width:400px; 9 height:300px; 10 background-color:#CCC; 11 }