springMVC的配置文件

来源:互联网 发布:手机软件网络错误 编辑:程序博客网 时间:2024/05/14 11:38

原文地址:http://blog.csdn.net/caochuankui/article/details/17389557

web.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <!-- spring 配置文件的加载 -->  
  11.   <context-param>  
  12.    <param-name>contextConfigLocation</param-name>  
  13.    <param-value>classpath:config/springAnnotation-*.xml</param-value>  
  14.   </context-param>  
  15.   <!-- 监听器 -->  
  16.   <listener>  
  17.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.   </listener>  
  19.   <!-- springMVC的配置 -->  
  20.   <servlet>  
  21.     <servlet-name>springMVC</servlet-name>  
  22.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  23.     <!-- 加载springMVC的配置文件 -->  
  24.     <init-param>  
  25.         <param-name>contextConfigLocation</param-name>  
  26.         <param-value>classpath:config/springAnnotation-servlet.xml</param-value>  
  27.     </init-param>  
  28.     <load-on-startup>1</load-on-startup>  
  29.   </servlet>  
  30.   <servlet-mapping>  
  31.     <servlet-name>springMVC</servlet-name>  
  32.     <url-pattern>/</url-pattern>  
  33.   </servlet-mapping>  
  34.   <!-- 过滤编码方式,防止乱码 -->  
  35.   <filter>  
  36.     <filter-name>encodingFilter</filter-name>  
  37.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  38.   </filter>  
  39.   <filter-mapping>  
  40.     <filter-name>encodingFilter</filter-name>  
  41.     <url-pattern>/*</url-pattern>  
  42.   </filter-mapping>  
  43. </web-app>  

spring基础配置

1.springAnnotation-core.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0 //EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"[  
  3. <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]>  
  4.   
  5. <beans>  
  6.     <import resource="classpath:spring/springAnnotation-import.xml"/>  
  7.       
  8. </beans>  

2.springAnnotation-hibernate.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0 //EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"[  
  3. <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]>  
  4.   
  5. <beans>  
  6.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >  
  7.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
  8.         <property name="url" value="jdbc:mysql://localhost:3306/information?useUnicode=true&characterEncoding=UTF-8"/>  
  9.         <property name="username" value="root"/>  
  10.         <property name="password" value="19880222"/>  
  11.     </bean>  
  12.       
  13.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  14.         <property name="dataSource" ref="dataSource" />  
  15.         <property name="hibernateProperties">  
  16.             <value>  
  17.                 hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  18.                 hibernate.show_sql=true  
  19.                 hibernate.format_sql=true  
  20.           </value>  
  21.         </property>  
  22.         <property name="configLocations">  
  23.             <list>  
  24.                 <value>  
  25.                 classpath:hibernate/hibernate.cfg.test.xml  
  26.                 </value>  
  27.             </list>  
  28.         </property>  
  29.     </bean>  
  30.       
  31.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  32.         <property name="sessionFactory" ref="sessionFactory"/>  
  33.     </bean>  
  34.     <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">  
  35.         <property name="transactionManager" ref="transactionManager"/>  
  36.         <property name="transactionAttributes">  
  37.             <props>  
  38.                 <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>  
  39.                 <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>  
  40.                 <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>  
  41.                 <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>  
  42.                 <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>  
  43.                 <prop key="get*">PROPAGATION_NEVER</prop>  
  44.             </props>  
  45.         </property>  
  46.     </bean>  
  47.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  48.         <property name="sessionFactory" ref="sessionFactory"/>  
  49.     </bean>  
  50.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  
  51. </beans>  

3.springAnnotation-servlet.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:context="http://www.springframework.org/schema/context"   
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.     http://www.springframework.org/schema/context  
  10.     http://www.springframework.org/schema/context/spring-context.xsd  
  11.     http://www.springframework.org/schema/mvc   
  12.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.     <!-- 加载包中的controller  注解扫描包 -->  
  14.    <context:component-scan base-package="com.xingao.controller"/>  
  15.    <!-- 开启注解 -->  
  16.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  17.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
  18.     <!-- 静态资源的访问 -->  
  19.     <mvc:resources location="/img/" mapping="/img/**"/>  
  20.     <mvc:resources location="/js/" mapping="/js/**"/>  
  21.       
  22.     <!-- 视图分解器 -->  
  23.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  24.         <property name="prefix" value="/"/>  
  25.         <property name="suffix" value=".jsp"/>  
  26.     </bean>  
  27.   
  28.     <!-- 上传文件的解析器 -->  
  29.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  30.         <property name="defaultEncoding" value="utf-8"/>  
  31.         <property name="maxUploadSize" value="10485760000"/>  
  32.         <property name="maxInMemorySize" value="40960"/>  
  33.     </bean>  
  34.       
  35. </beans>  

hibernate映射文件的加载

hibernate.cfg.test.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <mapping resource="com/xingao/demo/Message.hbm.xml"/>  
  11.     </session-factory>  
  12.   
  13. </hibernate-configuration>  

spring中bean的创建

springAnnotation-import.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <mapping resource="com/xingao/demo/Message.hbm.xml"/>  
  11.     </session-factory>  
  12.   
  13. </hibernate-configuration>  

这个是自己在学习springMVC时的一些配置文件,希望给以后学习这方面的人一些帮助,欢迎探讨指教。
0 0
原创粉丝点击