ITeye博客

Spring 总结04

wjjbfq · 2009-09-05 11:15 · Spring · Spring · Struts · Hibernate · Bean · Servlet · Markdown

Ssh整合Spring2.5+Hibernate3.3+Struts1.3整合开发

 

hibernate核心安装包下的:

hibernate3.jar

lib\required\*.jar

lib\optional\ehcache-1.2.3.jar

hibernate 注解安装包下的

lib\test\slf4j-log4j12.jar

Spring安装包下的

dist\spring.jar

dist\modules\spring-webmvc-struts.jar

lib\jakarta-commons\commons-logging.jar、commons-dbcp.jar、commons-pool.jar

lib\aspectj\aspectjweaver.jar、aspectjrt.jar

lib\cglib\cglib-nodep-2.1_3.jar

lib\j2ee\common-annotations.jar

lib\log4j\log4j-1.2.15.jar

Struts

下载struts-1.3.8-lib.zip,需要使用到解压目录下的所有jar,建议把jstl-1.0.2.jar和standard-1.0.2.jar更换为1.1版本。Spring中已经存在一个antlr-2.7.6.jar,所以把struts中的antlr-2.7.2.jar删除,避免jar冲突。

数据库驱动jar

 

**************************************************

Spring和hibernate

beans.xml配置文件

<!-- 1 spring整合hibernate的入口 创建本地化sessionFactory 加载hibernate.cfg.xml --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!--2 创建hibernate模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 3 配置hibernate的事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 4 定义通知 --> <tx:advice id="txadvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 5 配置切面 --> <aop:config> <aop:pointcut id="personPointcut" expression="execution(* com.itcast.service..*.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="personPointcut"/> </aop:config> <!-- *********************业务层和dao层的配置*******--> <!-- 配置dao层 --> <bean id="personDao" class="com.itcast.dao.impl.PersonDaoImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> <!-- 配置业务层 --> <bean id="personService" class="com.itcast.service.impl.PersonServiceImpl"> <property name="personDao"> <ref bean="personDao"/> </property> </bean> <!-- 配置struts action 整合struts时用 --> <bean name="/personAction" class="com.itcast.web.action.PersonAction"> <property name="personService"> <ref bean="personService"/> </property> </bean>


 

 

 *****************************************************************

struts1.3+Spring2.5整合开发(xml方式)

<!—1   配置struts-config.xml文件-->

<struts-config>  

<form-beans>

 <form-bean name="personForm" type="com.itcast.web.form.PersonForm" /> 

</form-beans>

    <action-mappings>

     <action path="/personAction" name="personForm" scope="request" parameter="method“

               type=“com.itcast.web.action.PersonAction”>

            <forward name="success" path="/success.jsp" />

       </action>

</action-mappings>

<!-- 把对struts中action的请求委托给spring容器管理,在spring的context配置文件中查找 -->

  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />

    <message-resources parameter="MessageResources" />

</struts-config>

<!—2   在spring的beans.xml文件中   配置struts action 的映射.name属性的值要与struts-config.xml文件中action元素的path属性相同,把action交给spring管理后,我们可以使用依赖注入在action中注入业务层的bean -->

   <bean name="/personAction" class="com.itcast.web.action.PersonAction">

       <property name="personService">

         <ref bean="personService"/>

       </property>

   </bean>

 <!--3   在struts-config.xml文件配置请求委托

             在struts配置文件中添加进spring的请求控制器,该请求控制器会先根据action的path属性值到  spring容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求-->

方法一    

<controller  processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />

方法二

 <controller> <set-property property="processorClass“

           value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller>

<!--4    在web.xml文件配置struts-->

<servlet>

      <servlet-name>action</servlet-name>

      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

      <init-param>

           <param-name>config</param-name>

           <param-value>/WEB-INF/struts-config.xml</param-value>

      </init-param>

       <load-on-startup>1</load-on-startup>

   </servlet>

 

    <servlet-mapping>

           <servlet-name>action</servlet-name>

           <url-pattern>*.do</url-pattern>

</servlet-mapping>

<!—5  在web容器中实例化spring容器-->

<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找   -->

<context-param>

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

      <param-value>/WEB-INF/classes/beans.xml</param-value> 

或 <param-value>classpath:beans.xml</param-value> //如有多个spring配置文件用逗号隔开

</context-param>

 

加载方法一

 <!-- 通过Listener配置Spring容器随Web应用的启动而初始化 -->
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

 

加载方法二

 <!-- 通过servlet配置Spring容器随Web应用的启动而初始化 -->
<servlet>

      <servlet-name>contextLoader</servlet-name>

      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

      <load-on-startup>2</load-on-startup>

</servlet>

 

使用spring解决struts1.3乱码问题。

<filter>

<filter-name>encoding</filter-name>

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

<init-param>

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

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encoding</filter-name>

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

</filter-mapping>

 

如果action没有交给spring管理时,我们通过下面语句获取spring容器实例

在struts的action中 可以使用:

WebApplicationContext webApplicationContext=

 WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());

 PersonService personService=(PersonService)webApplicationContext.getBean("personService");

 personService.savePerson(person);

*******************************************************

在hibernate中使用c3p0连接池

<!-- C3P0连接池设定-->

<!-- 使用c3po连接池 -->

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider

</property>

<!--在连接池中可用的数据库连接的最少数目 -->

<property name="c3p0.min_size">5</property>

<!--在连接池中所有数据库连接的最大数目  -->

<property name="c3p0.max_size">20</property>

<!--

设定数据库连接的过期时间,以秒为单位,

如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 

-->

<property name="c3p0.timeout">120</property>

 <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->

<property name="c3p0.idle_test_period">3000</property>

<!-- 设置自动提交 -->

<property name="connection.autocommit">true</property>

 

 

利用spring来管理session OpenSessionInView

<filter>
 <filter-name>Spring openEntityManagerInViewFilter</filter-name>
 <!-- if you are using hibernate 用于hibernate+spring-->
 <!-- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter -->
 <filter-class>
 org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter //这是集成jpa时所用
 <filter-class>
</filter>
<filter-mapping>
 <filter-name>Spring openEntityManagerInViewFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。