Spring MVC 配置

1、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7 
 8     <!-- 设置servlet编码 -->
 9     <filter>
10         <filter-name>springUtf8Encoding</filter-name>
11         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
12         <init-param>
13             <param-name>encoding</param-name>
14             <param-value>UTF-8</param-value>
15         </init-param>
16     </filter>
17     <filter-mapping>
18         <filter-name>springUtf8Encoding</filter-name>
19         <url-pattern>/*</url-pattern>
20     </filter-mapping>
21 
22     <!-- 加载程序上下文配置文件
23      如果contextConfigLocation参数没有设置,则加载默认的/WEB-INF/applicationContext.xml-->
24     <context-param>
25         <param-name>contextConfigLocation</param-name>
26         <param-value>classpath:applicationContext.xml</param-value>
27     </context-param>
28 
29     <listener>
30         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
31     </listener>
32 
33     <!-- 设置Spring servlet -->
34     <servlet>
35         <servlet-name>dispatcher</servlet-name>
36         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
37         <!-- 如果contextConfigLocation参数没有设置,则加载默认的/WEB-INF/dispatcher-servlet.xml -->
38         <init-param>
39             <param-name>contextConfigLocation</param-name>
40             <param-value>classpath:dispatcher-servlet.xml</param-value>
41         </init-param>
42         <load-on-startup>1</load-on-startup>
43     </servlet>
44 
45     <servlet-mapping>
46         <servlet-name>dispatcher</servlet-name>
47         <url-pattern>/</url-pattern>
48     </servlet-mapping>
49     
50 </web-app>

2、dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:mvc="http://www.springframework.org/schema/mvc"
 7        xsi:schemaLocation="
 8        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 
15     <!--  默认注解支持   -->
16     <mvc:annotation-driven />
17 
18     <!-- 如果当前请求为“/”时,则转发到“/home/hello” -->
19     <mvc:view-controller path="/" view-name="forward:/home/index"/>
20     
21     <!-- 静态资源映射 -->
22     <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
23     <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
24     <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
25     <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
26     <mvc:resources mapping="images/**" location="/WEB-INF/images/" />
27 
28 
29     <!-- 启动时扫描所有的controller -->
30     <context:component-scan base-package="com.demo.web.controllers" use-default-filters="false">
31         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
32     </context:component-scan>
33     
34     <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
35     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
36         <property name="prefix" value="/views/" />
37         <property name="suffix" value=".jsp" />
38     </bean>
39 
40     <!-- 支持上传文件 -->
41     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
42         <!-- 设置上传文件的最大尺寸为1MB -->
43         <property name="maxUploadSize">
44             <value>1048576</value>
45         </property>
46         <property name="defaultEncoding">
47             <value>UTF-8</value>
48         </property>
49     </bean>
50 
51 </beans>

 3、applicationContext.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xmlns:tx="http://www.springframework.org/schema/tx"
 5        xsi:schemaLocation="
 6   http://www.springframework.org/schema/beans
 7   http://www.springframework.org/schema/beans/spring-beans.xsd
 8   http://www.springframework.org/schema/tx
 9   http://www.springframework.org/schema/tx/spring-tx.xsd
10   http://www.springframework.org/schema/aop
11   http://www.springframework.org/schema/aop/spring-aop.xsd">
12 
13 <!-- 配置dataSource -->
14 <bean id="exampleDataSource" class="org.apache.commons.dbcp.BasicDataSource">
15     <property name="driverClassName" value="com.mysql.jdbc.Driver" />
16     <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
17     <property name="username" value="root" />
18     <property name="password" value="" />
19     <property name="initialSize" value="5" />
20     <property name="maxActive" value="10" />
21 </bean>
22 
23 <!-- 声明sessionFactory -->
24 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
25     <property name="dataSource" ref="dataSource"/>
26     <property name="mappingDirectoryLocations">
27         <list>
28             <value>classpath:com/demo/model/mappings</value>
29         </list>
30     </property>
31     <property name="hibernateProperties">
32         <props>
33             <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
34         </props>
35     </property>
36 </bean>
37 
38 <!-- 声明事务管理器开始 -->
39 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
40     <property name="sessionFactory" ref="sessionFactory"/>
41 </bean>
42 
43 <!-- 声明事务 -->
44 <tx:advice id="txAdvice" transaction-manager="txManager">
45     <tx:attributes>
46         <tx:method name="save*" propagation="REQUIRED" />
47         <tx:method name="update*" propagation="REQUIRED" />
48         <tx:method name="merge*" propagation="REQUIRED" />
49         <tx:method name="delete*" propagation="REQUIRED" />
50         <tx:method name="enable*" propagation="REQUIRED" />
51         <tx:method name="disable*" propagation="REQUIRED" />
52         <tx:method name="get*" propagation="REQUIRED" read-only="true" />
53         <tx:method name="count*" propagation="REQUIRED" read-only="true" />
54         <tx:method name="find*" propagation="REQUIRED" read-only="true" />
55         <tx:method name="list*" propagation="REQUIRED" read-only="true" />
56         <tx:method name="*" propagation="REQUIRED" read-only="true" />
57     </tx:attributes>
58 </tx:advice>
59 
60 <!-- 织入事务 -->
61 <aop:config>
62     <aop:pointcut id="txPointcut" expression="execution(* com.infrastructure.project.base.service..*.*(..))" />
63     <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
64 </aop:config>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。