Strut&Spring&Hibernate结合开发举例

来源:互联网 发布:东南大学seu网络 编辑:程序博客网 时间:2024/04/29 17:33

装载应用程序环境

Web.xml中添加:

<! 共有的spring注入可以配置在此处,以逗号分割 -->

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/
applicationContext.xml</param-value>
</context-param> 

 

struts-config.xml中添加

<! 如果多模块开发时,可以在每个struts-config.xml文件中指定各自的plugin -->

<plug-in className= "org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property= "contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
 </plug-in>

 

1,  使用 Spring ActionSupport

public class SearchSubmit extends ActionSupport {   |(1)

       ……

    ApplicationContext ctx =

      getWebApplicationContext();    |(2)

    BookService bookService =

      (BookService) ctx.getBean("bookService");   |(3)

       ……

}

2,  覆盖 RequestProcessor

struts-config.xml中添加

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> |(1)

 

spring-config.xml中增加

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>

 

<! name必须和strutspath一致,如果是子模块的path,必须在此处带上子模块路径 -->

  <bean name="/searchSubmit"

    class="ca.nexcel.books.actions.SearchSubmit"> |(1)

     <property name="bookService">

        <ref bean="bookService"/>

     </property>

  </bean>

</beans>

3,  将动作管理委托给 Spring

struts-config.xml中修改

<action path="/searchSubmit"

             type="org.springframework.web.struts.DelegatingActionProxy" |(1)

             input="/searchEntry.do"

             validate="true"

             name="searchForm">

             <forward name="success" path="/WEB-INF/pages/detail.jsp"/>

             <forward name="failure" path="/WEB-INF/pages/search.jsp"/>

</action>

 

spring-config.xml中增加

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>

 

<! name必须和strutspath一致,如果是子模块的path,必须在此处带上子模块路径 -->

  <bean name="/searchSubmit"  

        class="ca.nexcel.books.actions.SearchSubmit">

     <property name="bookService">

        <ref bean="bookService"/>

     </property>

  </bean>

</beans>

 

日志拦截:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
  <bean name="/searchSubmit" class="ca.nexcel.books.actions.SearchSubmit">
     <property name="bookService">
        <ref bean="bookService"/>
     </property>
  </bean>
  <!--  Interceptors --> 
  <bean name="logger"    
    class="ca.nexcel.books.interceptors.LoggingInterceptor"/> |(1)
  <!-- AutoProxies -->
  <bean name="loggingAutoProxy" class="org.springframework.aop.framework.autoproxy.
          BeanNameAutoProxyCreator"> |(2)
    <property name="beanNames">
          <value>/searchSubmit</valuesgt; |(3)
    </property>
    <property name="interceptorNames">
        <list>
          <value>logger</value> |(4)
        </list>
    </property>
   </bean>
</beans>

 

Spring日志的查看:

Log4j.properties文件放到WEB-INF/classes下面,可以查看日志详细信息

 

Hibernate的整合开发:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
<beans>
        <bean name="/userLogin" class="com.yourcompany.struts.action.UserLoginAction">
                        <property name="userLoginService">
                                       <ref bean="userLoginService"></ref>
                        </property>
        </bean>
 
        <bean id="userLoginService" class="com.yourcompany.business.UserLoginServiceImpl">
                        <property name="dao">
                                       <ref bean="TempUserDAO"></ref>
                        </property>
        </bean>
 
        <bean id="TempUserDAO" class="com.yourcompany.dao.TempUserDAO">
                        <property name="sessionFactory">
                                       <ref local="sessionFactory" />
                        </property>
        </bean>
 
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                        destroy-method="close">
                        <property name="driverClassName"
                                       value="oracle.jdbc.driver.OracleDriver">
                        </property>
                        <property name="url"
                                       value="jdbc:oracle:thin:@10.40.43.133:1521:zxin">
                        </property>
                        <property name="username" value="zxdbm_ismp"></property>
                        <property name="password" value="zxin_smap"></property>
        </bean>
 
        <bean id="sessionFactory"
                        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                        <property name="dataSource">
                                       <ref local="dataSource" />
                        </property>
                        <property name="hibernateProperties">
                                       <props>
                                                      <prop key="hibernate.dialect">
                                                                     org.hibernate.dialect.Oracle9Dialect
                                                      </prop>
                                                      <prop key="hibernate.show_sql">true</prop>
                                       </props>
                        </property>
                        <property name="mappingResources">
                                       <list>
                                                      <value>com/yourcompany/dao/TempUser.hbm.xml</value>
                                       </list>
                        </property>
        </bean>
 
        <bean id="transactionManager"
                        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                        <property name="sessionFactory">
                                       <ref local="sessionFactory" />
                        </property>
        </bean>
</beans>
 
报错:java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit

       删除 asm-2.2.3.jar