`
115893520
  • 浏览: 140421 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

struts2+spring2+hibernate3.1应用实例

阅读更多

1.         组合Hibernate Spring

1.1.     现在是创建一个新的项目,就要创建数据库

   oracle中创建一个product 的表,创建表的SQL如下

      CREATE TABLE PRODUCT

(

    ID                             NUMBER NOT NULL  PRIMARY KEY,

    NAME                          VARCHAR2(64),

    PRICE                          VARCHAR2(64),

    TYPE                           VARCHAR2(64),

    );

     

1.2.     Eclipse 中,新建一个Web project

    

 

1.3.     给项目增加Spring开发能力,增加spring相关类库到当前项目的lib,同时也提供了applicationContext.xml文件,注意:最好把applicationContext.xml文件保存到当前项目的WebRoot/WEB-INF的根目录下 

 

 

 

1.4.     给项目增加Hibernate开发能力,增加Hibernate相关类库到当前项目的lib,同时用applicationContext.xml文件代替hibernate,cfg.xml.

    

 

 

 

1.5.     通过MyEclipse的向导方式,生产POJO类和映射文件。

l         通过Show Views 找到DB Browser

l         选择user,单击右键,选择”Hibernate Revese Engineering..”

l         java src folder 选择正确工程下的:src,下面可以选择产生抽象的类abstract class,也可以不产生;hibernate3中可以选择产生DAO,也可以不选择,然后单击“Next”。

l         选择hibernate types,ID genrator 选择“native”。

添加后目录

     

          这时applicationContext.xml中,自动添加上了mappingResources

           <property name="mappingResources">

        <list>

              <value>com/cnkf/template/pojo/Product.hbm.xml</value></list>

        </property>

1.6.     添加好实体类,添加DAO类、Service类和相应的接口,

             

1.7.     修改applicationContext.xml,添加daoservice配置。

     <bean id="dao"

      class="com.cnkf.template.dao.ProductDAOImpl">

      <property name="sessionFactory" >

          <ref bean="sessionFactory"/>

      </property>

   </bean>

   <bean id="productServ"

      class="com.cnkf.template.service.ProductServImpl">

      <property name="dao" >

          <ref bean="dao"/>

      </property>

</bean>

2.         在组合struts2 ,完成struts,hibernate,spring三者的组合。

2.1.     修改web,.xml,增加struts 所需的过滤器配置

<!--在部署之前,先清除环境,如不加,查看视频是会找不到资源-->

        <filter-name>struts-cleanup</filter-name>

        <filter-class>

         org.apache.struts2.dispatcher.ActionContextCleanUp

        </filter-class>

    </filter>

       <!--部署filter的名称以及对应的类-->

      <filter>

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

        <filter-class>

            org.apache.struts2.dispatcher.FilterDispatcher

        </filter-class>

    </filter>

       <!--部署filter的对应的URL模式-->

    <filter-mapping>

        <filter-name>struts-cleanup</filter-name>

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

    </filter-mapping>

   <filter-mapping>

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

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

   </filter-mapping>

   <filter-mapping>

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

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

   </filter-mapping>

   <filter-mapping>

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

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

</filter-mapping>

项目中还可能用到其他的过滤器,开发中自行添加

2.2.     添加struts2必须的类库,增加struts2-spring-plugin-x-x-x.jar文件,

commons-logging-1.0.4.jarfreemarker-2.3.12.jarognl-2.6.11.jarstruts2-core-2.1.2.jarxwork-2.1.1.jar

 

2.3.     需要拷贝struts.xml, struts.propertiessrc目录下,然后进行修改

struts.xml文件

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

     <!--需要用到struts-default.xml的配置,就必须加载-->

   <include file="struts-default.xml"/>

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

     </package>

      <!--struts属性的设置-->

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

    <!-- 此处用constant元素定义常量 -->

   <constant name="struts.devMode" value="true"/>

   <!-- 定义资源文件的位置和类型 -->

   <constant name="struts.custom.i18n.resources" value="properties/myMessages"/>

   <!-- 设置应用使用的解析码 -->

   <constant name="struts.i18n.encoding" value="UTF-8"/>

   <!-- 设置应用使用的上传解析器类型 -->

   <constant name="struts.multipart.parser" value="jakarta"/>

   <!-- 指定使用按type的自动装配策略 -->

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

</struts>

struts.properties

     struts.locale=en_GB

  有需要在添加。

2.4.     修改web,.xml,增加spring的监听器和上下文变量

    <context-param>

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

   <param-value>/WEB-INF/applicationContext*.xml</param-value>

   </context-param>

   <listener>

      <listener-class>

   org.springframework.web.context.ContextLoaderListener

      </listener-class>

  </listener>

2.5.     修改web,.xml,增加OpenSessionInViewFilter的设置

   <filter>

      <filter-name>lazyLoadingFilter</filter-name>

<filter-class>  org.springframework.orm.hibernate3.support.OpenSession InViewFilter

      </filter-class>

 

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics