SSH2整合入门

1.     配置文件的解决

配置spring的监听器

web.xml

<listener>     

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

用srping管理sessionFactory,在 HibernateTemplate里面注入一个sessionfactory的引用,HibernateTemplate比sessionFactory强大很多

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate>

   <property name="sessionFactory" ref="sessionFactory"></property>

</bean>

在web.xml对spring配置文件的初始化

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value><!--这个是默认的-->

  <param-value>classpath:beans.xml</param-value>

</context-param>

我们还要导入struts2-spring-plugin.jar和加入struts-plugin.xml

sturts读常量的顺序:

1)struts-default.xml

2)struts-plugin.xml

3)struts.xml

4)struts.properties

5)web.xml

这里加入struts-spring-plugin.jar和struts-plugin.xml之后,加入struts.xml中有<action>,它会默认把队友class里面的类给扫描到容器了.所以你在那个类不写@Component(“xxx”) ,@Scope(“prototype”)也行,写了@Resource(name=”newName”)也没有用.这个问题好好好注意

如果想把主动权交给spring,只能在属性上使用@Resource(name=”newName”);

2.     一个常见错误的解决方法:

用load()方法的时候很容易出现的一个错误就是:.AbstractLazyInitializer.initialize就是使用load()方法的时候session已经关闭了.

我们可以在web.xml中加入一个新的过滤器,扩大session的范围

<filter>

  <filter-name>openSessionInView</filter-name>

  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

<!—这个filter默认找的是名字为sessionFactory的session工厂-->

<init-param>

<param-name>sessionFactoryBeanName</param-name>

<param-value>sf</param-value>

<!—指定新的名字-->

</init-param>

</filter>

 

<filter-mapping>

  <filter-name>openSessionInView</filter-name>

  <url-pattern>/*</url-pattern>

</filter-mapping>

这个过滤器一定要放在struts过滤器的前面

而且使用这个filter之后,它拦截到一个方法,假如没有配置事务,它会认为这个事务的属性是read-only

3.     乱码的问题:插入数据库的信息假如出现了乱码.可能的情况很多.要逐一排除

struts2.1.6的i18n是一个bug

struts2.1.8进行了修正,如果是struts2.1.8就直接设置i18n的属性值就行了

struts2.1.6则可以用它提供的一个转码类

web.xml加入

<filter>

  <filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter.class</filter-class>

  <init-param>

    <param-name>encoding</param-name>

    <param-value>GBK</param-value>

  </init-param>

</filter>

<filter-mapping>

  <fiter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

<filter-mapping>

在spring中提供了一个测试类,用这个测试类的好处是不破坏数据

4.     service和dao层的区别:有时候感觉service和dao没有什么区别

dao完成连接数据库修改删除添加等的实现细节,例如sql语句是怎么写的,怎么把对象放入数据库的
service层是面向功能的,一个个功能模块比如说银行登记并完成一次存款,UI要把请求给service层,然后service曾将这一个case分解成许多步骤调用底层的实现完成这次存款,dao就是下面那层

dao就是把数据存起来,之所以service的方法会有雷同只不过是因为service得需求不是很复杂不用再service里面完成太多包装或者处理过程可以直接调用dao的方法就完成的请求处理例如就要save一个对象,而这个对象是封装好的,dao里面有个方法专门save封装好的对象于是service的方法就仅仅调用一下就o了,函数签名自然很像了
service不能直接接触持久层,而dao是持久层或者直接访问持久层
有的时候只是为了分层清楚,为了将来scale up的时候方便我们才把service和dao分开,其实没必要分开的

5.     最后写下这两个基本配置文件:

application.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-2.5.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">

    <!--default-autowire="byName" 定义注入依赖通过 对象名称-->

    <!--Spring2.5中基于注释的依赖注入-->

    <context:annotation-config/>

    <!-- 事务配置,注册Spring实现的为hiberante而写的 HibernateTransactionManager-->

    <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

    <!-- 使用annotation定义事务,事务管理者为前面定义HibernateTransactionManager,并且对目标类生成代理-->

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    <!-- 使用annotation 自动注册bean,从指定的包下 -->

    <context:component-scan base-package="cn.com" />

    <!--指定数据源-->

    <bean id="dataSource"

       class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName"

           value="oracle.jdbc.OracleDriver">

       </property>

       <property name="url"

           value="jdbc:oracle:thin:@guest753:1521:accp">

       </property>

       <property name="username" value="scott"></property>

       <property name="password" value="tiger"></property>

    </bean>

    <!--注册sessionFactory-->

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

       <property name="dataSource">

           <ref bean="dataSource" />

       </property>

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.Oracle9iDialect

              </prop>

              <prop key="hibernate.show_sql">true</prop>

           </props>

       </property>

       <!--定义扫描那些包下的类作为entity实体对象管理-->

       <property name="packagesToScan">

           <list>

              <value>cn.com.entity.*</value>

           </list>

       </property>

    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">

  <display-name>Stu</display-name>

    <!-- struts2支持 -->

    <filter>

       <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

       <init-param>

           <param-name>actionsPackages</param-name>

           <param-value>cn.com.action</param-value>

       </init-param>

    </filter>

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

    <!-- 添加监听器 -->

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext*.xml</param-value>

    </context-param>

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

  </welcome-file-list>

</web-app>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值