web.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 6 version="3.1"> 7 8 <servlet> 9 <servlet-name>dispatcher</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 14 </init-param> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 18 <servlet-mapping> 19 <servlet-name>dispatcher</servlet-name> 20 <url-pattern>/</url-pattern> 21 </servlet-mapping> 22 23 <context-param> 24 <param-name>contextConfigLocation</param-name> 25 <param-value>classpath:applicationContext.xml</param-value> 26 </context-param> 27 28 <listener> 29 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 30 </listener> 31 </web-app>
applicationContext.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context.xsd" 15 default-init-method="init" default-destroy-method="destroy"> 16 17 <!-- 导入外部的properties文件 --> 18 <context:property-placeholder location="classpath:jdbc.properties"/> 19 <!--<context:annotation-config/>--><!--启用注解扫描--> 20 21 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!--数据源--> 22 <property name="url" value="${jdbc.url}"/> 23 <property name="driverClassName" value="${jdbc.driver}"/> 24 <property name="username" value="${jdbc.name}"/> 25 <property name="password" value="${jdbc.pass}"/> 26 </bean> 27 28 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 29 <property name="dataSource" ref="dataSource"/> 30 <property name="hibernateProperties"> 31 <props> 32 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 33 <prop key="hibernate.show_sql">true</prop> 34 </props> 35 </property> 36 <property name="mappingResources"> 37 <list> 38 <value>hbm/User.hbm.xml</value> 39 <value>hbm/UserDetails.hbm.xml</value> 40 <value>hbm/Goods.hbm.xml</value> 41 <value>hbm/GoodDetails.hbm.xml</value> 42 <value>hbm/BuyCar.hbm.xml</value> 43 <value>hbm/Order.hbm.xml</value> 44 <value>hbm/OrderDetails.hbm.xml</value> 45 </list> 46 </property> 47 </bean> 48 49 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 50 <property name="sessionFactory" ref="sessionFactory"/> 51 </bean> 52 53 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 54 <tx:attributes> 55 <tx:method name="create*"/> 56 <tx:method name="query*"/> 57 <tx:method name="update*"/> 58 <tx:method name="delete*"/> 59 </tx:attributes> 60 </tx:advice> 61 62 <aop:config> 63 <!--切面的配置 id表示切面的唯一id,ref表示切面的支持类--> 64 <aop:pointcut id="CRUDOperate" expression="execution(* com.iotek.homework.service.*.*(..))"/><!--切点( 切入点)--> 65 <aop:advisor advice-ref="txAdvice" pointcut-ref="CRUDOperate"/> 66 </aop:config> 67 68 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> 69 <property name="sessionFactory" ref="sessionFactory"/> 70 </bean> 71 72 <!--user start--> 73 <bean id="userDAO" class="com.iotek.homework.dao.UserDAOImpl"/> 74 <bean id="userService" class="com.iotek.homework.service.UserServiceImpl"/> 75 <bean id="userController" class="com.iotek.homework.controller.UserController"/> 76 <!--user end--> 77 78 <!--userDetails start--> 79 <bean id="userDetailsDAO" class="com.iotek.homework.dao.UserDetailsDAOImpl"/> 80 <bean id="userDetailsService" class="com.iotek.homework.service.UserDetailsServiceImpl"/> 81 <!-- <bean id="userDetailsAction" class="com.iotek.homework.actions.UserDetailsAction"/>--> 82 <!--userDetails end--> 83 84 <!--goods start--> 85 <bean id="goodsDAO" class="com.iotek.homework.dao.GoodsDAOImpl"/> 86 <bean id="goodsService" class="com.iotek.homework.service.GoodsServiceImpl"/> 87 <!-- <bean id="goodsAction" class="com.iotek.homework.actions.GoodsAction"/>--> 88 <!--goods end--> 89 90 <!--buyCar start--> 91 <bean id="buyCarDAO" class="com.iotek.homework.dao.BuyCarDAOImpl"/> 92 <bean id="buyCarService" class="com.iotek.homework.service.BuyCarServiceImpl"/> 93 <!-- <bean id="buyCarAction" class="com.iotek.homework.actions.BuyCarAction"/>--> 94 <!--buyCar end--> 95 96 <!--orders start--> 97 <bean id="ordersDAO" class="com.iotek.homework.dao.OrdersDAOImpl"/> 98 <bean id="ordersService" class="com.iotek.homework.service.OrdersServiceImpl"/> 99 <!-- <bean id="ordersAction" class="com.iotek.homework.actions.OrdersAction"/>--> 100 <!--orders end--> 101 102 </beans>
dispatcher-servlet.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 <mvc:annotation-driven /> 15 <!--注解扫描--> 16 <context:component-scan base-package="com.iotek.homework.controller"/> 17 18 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <property name="prefix" value="/pages/"/> 20 <property name="suffix" value=".jsp"/> 21 </bean> 22 23 <bean id="jsonConvert" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 24 25 <bean id="stringConvert" class="org.springframework.http.converter.StringHttpMessageConverter"> 26 <property name="supportedMediaTypes"> 27 <list> 28 <value>text/plain;charset=UTF-8</value> 29 </list> 30 </property> 31 </bean> 32 33 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 34 <property name="messageConverters"> 35 <list> 36 <ref bean="jsonConvert"/> 37 <ref bean="stringConvert"/> 38 </list> 39 </property> 40 </bean> 41 42 <import resource="classpath:applicationContext.xml"/> 43 44 <!--静态资源注册--> 45 <mvc:resources mapping="/css/*" location="/css/"/> 46 <mvc:resources mapping="/js/*" location="/js/"/> 47 <mvc:resources mapping="/images/*" location="/images/"/> 48 </beans>