struts2 spring hibernate3 的最精细的整合

来源:互联网 发布:windows rt8.1 编辑:程序博客网 时间:2024/05/16 15:30

整合struts2 spring Hibernate 的整合 最简单精细的整合

            

          一: 新建web工程

          二: 拷贝必要的包

 

 http://hi.csdn.net/space-2177796-do-album-picid-489456.html

 

      三:struts 配置(文件模板)

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

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

 

<constant name="struts.objectFactory" value="spring"></constant>

<!—对于struts2.1.8来说上面的配置可有可无-->

    <package name="default" namespace="/" extends="struts-default">

       <action name="test" class="test" method="exec">

           <result name="success">/success.jsp</result>

       </action>

      

       <action name="reg" class="reg" method="reg">

           <result name="success">/reg-success.jsp</result>

       </action>

      

    </package>

 

</struts>

 

四:spring 配置文件:(模板文件,如何像下面那样,Hibernate的配置文件就不要了)

 

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

<!--

    - Middle tier application context definition for the image database.

-->

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

 

    <bean id="dataSource"

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

       destroy-method="close">

       <property name="driverClassName"

           value="com.mysql.jdbc.Driver">

       </property>

       <property name="url"

           value="jdbc:mysql://localhost:3306/itcast">

       </property>

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

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

       <property name="maxActive" value="100"></property>

       <property name="maxIdle" value="30"></property>

       <property name="maxWait" value="500"></property>

       <property name="defaultAutoCommit" value="true"></property>

 

 

    </bean>

 

    <bean id="sessionFactory"

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

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

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.MySQLDialect

              </prop>

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

 

           </props>

       </property>

       <property name="mappingResources">

           <list>

              <value>com/test/bean/User.hbm.xml</value>

           </list>

       </property>

    </bean>

 

    <bean id="userDao" class="com.test.dao.UserDaoImpl">

       <property name="sessionFactory">

           <ref bean="sessionFactory" />

       </property>

    </bean>

  

    <bean id="reg" class="com.test.action.RegAction">

       <property name="userDao">

           <ref bean="userDao" />

       </property>

    </bean> 

 

</beans>



五:web.xml 的配置

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

<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

    <display-name>Struts Blank</display-name>

 

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

 

    <welcome-file-list>

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

    </welcome-file-list>

   

    <listener>

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

    </listener>

 

</web-app>

 

六:测试吧 :我做的一个简单的工程,下面是缩略图:

 

 

没有什么验证功能,可以自己加上去。

http://hi.csdn.net/space-2177796-do-album-picid-489458.html

 

原创粉丝点击