spring MVC配置文件

一.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.   <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
     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0 //EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"[
     <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]>

     <beans>
              <import resource="classpath:spring/springAnnotation-import.xml"/>

     </beans>
  2.springAnnotation-hibernate.xml
     <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0 //EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"[
          <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]>

     <beans>
         <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
             <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
             <property name="url" value="jdbc:mysql://localhost:3306/information?useUnicode=true&characterEncoding=UTF-8"/>
             <property name="username" value="root"/>
             <property name="password" value="19880222"/>
         </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
             <property name="dataSource" ref="dataSource" />
             <property name="hibernateProperties">
                 <value>
                     hibernate.dialect=org.hibernate.dialect.MySQLDialect
                     hibernate.show_sql=true
                     hibernate.format_sql=true
                 </value>
             </property>
             <property name="configLocations">
                 <list>
                     <value>
                     classpath:hibernate/hibernate.cfg.test.xml
                     </value>
                 </list>
             </property>
         </bean>

         <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
             <property name="sessionFactory" ref="sessionFactory"/>
         </bean>
         <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
             <property name="transactionManager" ref="transactionManager"/>
             <property name="transactionAttributes">
                 <props>
                     <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
                     <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
                     <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
                     <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
                     <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
                     <prop key="get*">PROPAGATION_NEVER</prop>
                 </props>
                  </property>
              </bean>
              <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
                       <property name="sessionFactory" ref="sessionFactory"/>
              </bean>
              <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
     </beans>
3.springAnnotation-servlet.xml

     <?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 加载包中的controller  注解扫描包 -->
   <context:component-scan base-package="com.xingao.controller"/>
   <!-- 开启注解 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <!-- 静态资源的访问 -->
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

    <!-- 视图分解器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 上传文件的解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10485760000"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

</beans>
三. hibernate映射文件的加载( hibernate.cfg.test.xml )

     <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <mapping resource="com/xingao/demo/Message.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

四. spring中bean的创建
     
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <mapping resource="com/xingao/demo/Message.hbm.xml"/>
    </session-factory>

</hibernate-configuration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值