zoukankan      html  css  js  c++  java
  • SpringMVC配置文件

    第一步:导入相关jar包 springMVC+hibernate

    第二步:web-inf下的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_2_5.xsd" id="WebApp_ID" version="2.5">
     3   <display-name>json_test</display-name>
     4   <welcome-file-list>
     5     <welcome-file>login.jsp</welcome-file>
     6   </welcome-file-list>
     7   
     8   <!-- 一个web.xml中可以配置多个DispatcherServlet,通过 servlet-mapping的不同设置,让每个DispatcherServlet处理不同的请求-->
     9   
    10   <!-- 业务层和持久层的bean的spring配置文件。applicationContext.xml.多个配置文件使用,号隔开-->
    11   <context-param>
    12       <param-name>contextConfigLocation</param-name>
    13       <param-value>classpath*:config/spring/spring-*.xml</param-value>
    14   </context-param>
    15   
    16   <!-- 配置Spring监听 。通过contextConfigLocation配置的xml文件启动业务层(service和dao)的bean的容器。【service层和dao层的容器】-->
    17   <listener>
    18       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    19   </listener>
    20   
    21   <!-- 配置SpringMVC的DispatcherServlet ,它是springmvc的灵魂和心脏,它协调各组件完成一次完整的请求响应-->
    22   <!-- (默认自动加载web-inf下的<servltname>-servlet.xml的spring配置文件)启动web层的spring容器【控制器,请求分发器】 -->
    23   <!-- 如果配置init-param则是打破默认自动加载,而是按param-value中的路径,加载web层容器 -->
    24   <!-- web层的spring容器是业务层的spring容器的子容器:即web层容器中的bean【controller】可以调用业务层bean【service和dao】而业务层bean调用不到web层的bean -->
    25   <servlet>
    26       <servlet-name>springMVC</servlet-name>
    27       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    28       <init-param>
    29       <!-- 装配webApplicationContext容器。其实ApplicationContext容器的子类-->
    30           <param-name>contextConfigLocation</param-name>
    31           <param-value>classpath*:config/spring/spring-mvc.xml</param-value>
    32       </init-param>
    33       <load-on-startup>1</load-on-startup>
    34   </servlet>
    35   <servlet-mapping>
    36       <servlet-name>springMVC</servlet-name>
    37       <url-pattern>/</url-pattern>
    38   </servlet-mapping>
    39   
    40   <!-- 配置字符集 -->
    41   <filter>
    42       <filter-name>encodingFilter</filter-name>
    43       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    44       <init-param>
    45           <param-name>encoding</param-name>
    46           <param-value>UTF-8</param-value>
    47       </init-param>
    48       <init-param>
    49           <param-name>forceEncoding</param-name>
    50           <param-value>true</param-value>
    51       </init-param>
    52   </filter>
    53   <filter-mapping>
    54       <filter-name>encodingFilter</filter-name>
    55       <url-pattern>/*</url-pattern>
    56   </filter-mapping>
    57   
    58   <!-- 配置Session,扩大session的生命周期 -->
    59   <filter>
    60       <filter-name>openSession</filter-name>
    61       <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    62   </filter>
    63   <filter-mapping>
    64       <filter-name>openSession</filter-name>
    65       <url-pattern>/*</url-pattern>
    66   </filter-mapping>
    67 </web-app>
    View Code

    spring-mvc.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-3.2.xsd
    10     http://www.springframework.org/schema/mvc
    11     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    12     
    13     <!--此xml是springMvc是启动控制器和视图解析工具的 -->
    14     
    15     <!-- 注解扫描包 pojo处定义@Controller,此配置扫描相应的类包,将有注解的pojo变成一个可以分发http请求的控制器-->
    16     <!-- @Controller将pojo变成可以分发http请求的控制器。 -->
    17     <!-- @RequestMapping确定不同请求,调用不同控制器中的某个控制器,然后调用某个控制器中的某个方法 -->
    18     <!-- 类头定义@RequestMapping是提供初步的请求映射信息(相对于项目部署),方法头定义@RequestMapping(相对于类头部署)是确定请求所要调用的方法。请求规则包含:url,请求参数,请求方法,请求头 -->
    19     <context:component-scan base-package="com.tgb.web" />
    20     
    21     <!-- <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 
    22             会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
    23             并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
    24            后面,我们处理响应ajax请求时,就使用到了对json的支持。后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping
    25            与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。-->
    26     <mvc:annotation-driven />
    27     
    28     
    29     <!-- 静态资源(js/image)的访问 -->
    30     <mvc:resources location="/js/" mapping="/js/**"/>
    31     
    32     
    33     <!-- 定义视图解析器 -->    
    34     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    35         <property name="prefix" value="/"></property>
    36         <property name="suffix" value=".jsp"></property>
    37     </bean>
    38 </beans>
    View Code

    spring-common.xml装配ioc容器。连接数据库信息。配置切面,给业务类加事务

     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     
     9     <!-- 配置数据源 -->
    10     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    11         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    12         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
    13         <property name="username" value="root"></property>
    14         <property name="password" value="1234"></property>
    15     </bean>
    16     
    17     <!-- 配置SessionFactory -->
    18     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    19         <property name="dataSource" ref="dataSource" />
    20         <property name="hibernateProperties">
    21             <props>
    22                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    23                 <prop key="hibernate.hbm2ddl.auto">update</prop>
    24                 <prop key="hibernate.show_sql">true</prop>
    25                 <prop key="hibernate.format_sql">true</prop>
    26             </props>
    27         </property>
    28         
    29         <!-- 【3】mapping数据模型和数据库的桥梁,只需要配置到路径,无需一个个配置 .hibernate的映射文件 类名.hbm.xml-->
    30             <property name="mappingDirectoryLocations">
    31                 <list>
    32                     <value>classpath:/com/tgb/entity</value>
    33                 </list>
    34             </property>
    35         
    36         <!--  注解方式扫描实体类
    37         <property name="annotatedClasses">
    38             <list>
    39                 <value>com.tgb.entity.User</value>
    40             </list>
    41         </property>
    42         -->
    43     </bean>
    44     
    45     <!-- 配置springMVC上传文件和下载文件 -->
    46     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    47         <property name="defaultEncoding" value="utf-8"></property><!-- 编码格式,与jsp格式一致 -->
    48         <property name="maxUploadSize" value="52428880"></property><!-- 文件上传的最大限制 5mb -->
    49         <property name="uploadTempDir" value="upload/temp"></property><!-- 上传文件的临时路径。文件上传后,会自动消失 -->
    50         <property name="maxInMemorySize" value="4096"/> 
    51     </bean>
    52     
    53     
    54     
    55     
    56     <!-- 配置一个事务管理器 -->
    57     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    58         <property name="sessionFactory" ref="sessionFactory"/>
    59     </bean>
    60     
    61     <!-- 配置事务,使用代理的方式 -->
    62     <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">  
    63         <property name="transactionManager" ref="transactionManager"></property>  
    64         <property name="transactionAttributes">  
    65             <props>  
    66                 <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>  
    67                 <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>  
    68                 <prop key="query*">PROPAGATION_REQUIRED,-Exception</prop>  
    69                 <prop key="get*">PROPAGATION_REQUIRED,-Exception</prop>  
    70                 <prop key="count*">PROPAGATION_REQUIRED,-Exception</prop>  
    71                 <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>  
    72                 <prop key="del*">PROPAGATION_REQUIRED</prop>  
    73                 <prop key="*">PROPAGATION_REQUIRED</prop>  
    74             </props>  
    75         </property>  
    76     </bean> 
    77 </beans>
    View Code

    spring-beans.xml给业务类加上事务。也可用ssh配置事务的方法

     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     <!-- 此处为代理 ,为业务类设置事务-->
     9     <bean name="userManager" parent="transactionProxy">
    10         <property name="target" ref="userManagerBase"></property>
    11     </bean>    
    12     
    13     <bean name="studentService" parent="transactionProxy">
    14         <property name="target" ref="studentServiceBase"></property>
    15     </bean>
    16     <bean name="classRoomService" parent="transactionProxy">
    17         <property name="target" ref="classRoomServiceBase"></property>
    18     </bean>
    19 </beans>
    View Code

    spring-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     
     9     <bean id="userManagerBase" class="com.tgb.service.serviceImpl.UserManagerImpl" scope="prototype" autowire="byType"></bean>
    10     <bean id="studentServiceBase" class="com.tgb.service.serviceImpl.StudentServiceImpl" scope="prototype" autowire="byType"></bean>
    11     <bean id="classRoomServiceBase" class="com.tgb.service.serviceImpl.ClassRoomServiceImpl" scope="prototype" autowire="byType"></bean>
    12 </beans>
    View Code

    第七步:spring-dao.xml配置项目中dao层的对象

     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     
     9 
    10     
    11     <bean id="userDao" class="com.tgb.dao.daoImpl.UserDaoImpl" scope="prototype">
    12         <property name="sessionFactory" ref="sessionFactory"></property>
    13     </bean>
    14     <bean id="studentDao" class="com.tgb.dao.daoImpl.StudentDaoImpl"  scope="prototype">
    15         <property name="sessionFactory" ref="sessionFactory"></property>
    16     </bean>
    17     <bean id="classRoomDao" class="com.tgb.dao.daoImpl.ClassRoomDaoImpl" scope="prototype">
    18         <property name="sessionFactory" ref="sessionFactory"></property>
    19     </bean>
    20 </beans>
    View Code

     

     

     

     

  • 相关阅读:
    Windows c++面向对象与可视化编程的基础知识
    Windows c++程序的基本结构
    云计算
    分布式计算
    从贝叶斯方法谈到贝叶斯网络
    C++函数调用
    插入排序
    Http请求中请求头Content-Type讲解
    5种PHP创建数组的方式
    js数组
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/4065469.html
Copyright © 2011-2022 走看看