• 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" xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:aop="http://www.springframework.org/schema/aop" 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/context 
     9         http://www.springframework.org/schema/context/spring-context.xsd
    10         http://www.springframework.org/schema/tx 
    11         http://www.springframework.org/schema/tx/spring-tx.xsd
    12         http://www.springframework.org/schema/aop 
    13         http://www.springframework.org/schema/aop/spring-aop.xsd">
    14 
    15     <!-- 配置数据源 dbcp数据源 -->
    16     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    17         <property name="driverClassName" value="${driverClass}" />
    18         <property name="url" value="${jdbcUrl}" />
    19         <property name="username" value="${user}" />
    20         <property name="password" value="${password}"/>
    21     </bean>
    22 
    23     <!-- 使用配置文件 加载 数据库需要的4要素 经常使用 -->
    24     <context:property-placeholder location="classpath:jdbc.properties" />
    25     
    26     
    27     <!--配置sessionFactory -->
    28      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    29         <!-- 读取hibernate配置文件<property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
    30          <!-- 配置数据源 -->
    31          <property name="dataSource" ref="dataSource"></property>
    32          <!-- 配置映射文件  Teacher.hbm.xml -->
    33          <property name="mappingDirectoryLocations" value="cn/bdqn/bean"/>
    34          <property name="hibernateProperties">
    35           <props>
    36             <prop key="hibernate.hbm2ddl.auto">update</prop>
    37             <prop key="hibernate.show_sql">true</prop>
    38             <prop key="hibernate.format_sql">true</prop>
    39             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
    40           </props>
    41          </property>
    42      </bean>
    43 
    44   <!--配置dao层  -->
    45   <bean  id="teacherDao" class="cn.bdqn.dao.impl.TeacherDaoImpl">
    46   <property name="sessionFactory" ref="sessionFactory"/>
    47 </bean>
    48 
    49  <!--配置service层  -->
    50   <bean  id="teacherService" class="cn.bdqn.service.impl.TeacherServiceImpl">
    51     <property name="dao" ref="teacherDao"></property>
    52   </bean>
    53 
    54 <!-- 配置action层 -->
    55 
    56 <!-- ==========================事务======================================= -->
    57 <!--  配置事务管理器  -->
    58   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    59     <property name="dataSource" ref="dataSource"/>
    60   </bean>
    61   
    62   
    63   <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--  设置事务的通知  -->
    64       <tx:attributes>
    65       <!-- 对连接点上的方法进行事务属性的配置 -->
    66        <tx:method name="add*"  isolation="DEFAULT" propagation="REQUIRED"/>
    67        <tx:method name="delete*"  isolation="DEFAULT" propagation="REQUIRED"/>
    68        <tx:method name="update*"  isolation="DEFAULT" propagation="REQUIRED"/>
    69        <tx:method name="find*"  read-only="true" isolation="DEFAULT" propagation="REQUIRED"/>
    70       </tx:attributes>
    71   </tx:advice>
    72   
    73   <aop:config>
    74       <!-- 指定切入点 -->
    75      <aop:pointcut expression="execution(* *..service.*.*(..))" id="myPoint"/>
    76      <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint"/>
    77   </aop:config>
    78 
    79 </beans>
    applicationContext.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   <display-name></display-name>
     8   
     9   
    10   <!-- 配置全局监听器 确保 容器 对象 只被实例化一次! -->
    11    <listener>
    12     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    13    </listener>
    14   <!--  默认xml名称 必须是 applicationContext.xml 必须在 WEB-INF的根目录下
    15      现在我们 设置applicationContext.xml文件的路径     我们也可以更改 xml文件的名称 -->
    16    <context-param>
    17     <param-name>contextConfigLocation</param-name>
    18     <param-value>classpath:applicationContext.xml</param-value>
    19   </context-param>
    20 
    21   
    22   <servlet>
    23     <servlet-name>AddServlet</servlet-name>
    24     <servlet-class>cn.bdqn.servlet.AddServlet</servlet-class>
    25   </servlet>
    26 
    27   <servlet-mapping>
    28     <servlet-name>AddServlet</servlet-name>
    29     <url-pattern>/AddServlet</url-pattern>
    30   </servlet-mapping>
    31   

    在 dao层以及service层创建对应的 根据ID获取Teacher的信息!
    使用get方式 不会报错!
    但是使用load方式获取数据的话,就会出现No  Session!
    因为在前台真正获取数据的时候,事务已经提交了,session也关闭了!
    这时候 我们需要配置 OpenSessionInView模式!来保证用户获取数据的时候还有session!
    只需要在web.xml文件中配置如下节点:
      
       <!-- 设置openSessionInView  必须在struts2的核心控制器 之前  不然会起作用 -->
       <filter>
       <filter-name>open</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
      </filter>
     
       <filter-mapping>
         <filter-name>open</filter-name>
         <url-pattern>/*</url-pattern>
       </filter-mapping>
     
     
      <!--配置struts2的核心控制器  -->
         <filter>
       <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
     
       <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
       </filter-mapping>
     
     

    OpenSessionInView模式


    32 <welcome-file-list> 33 <welcome-file>index.jsp</welcome-file> 34 </welcome-file-list> 35 </web-app>
     1 <?xml version="1.0" encoding="UTF-8"?>
     2  <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
     4     "http://struts.apache.org/dtds/struts-2.1.dtd">
     5 
     6 <struts>
     7     <package name="default" namespace="/" extends="struts-default">
     8         <!-- 每次请求AddServlet 都会实例化一个 对象    需要让spring容器去管理 -->
     9         <action name="AddServlet" class="teacherAction" method="add">
    10             <result>/success.jsp</result>
    11         </action>
    12     </package>
    13 
    14 </struts>
  • 相关阅读:
    关于 “支持“XXXDBContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库” 的解决办法
    ASP.NET MVC 添加Model并连接到本地数据库
    C#中的if if...和if-else if
    Git和Github的使用教程
    JavaEE和Tomcat环境
    将windows系统新建的文本文档默认编码为UTF-8
    WIN10缺少.NET Framework3.5解决办法
    上传图片预览设置src不显示
    IOS new Date() 时间转换失败问题以及其他问题
    是否支持css3
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/6890145.html
Copyright © 2020-2023  润新知