免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1597 | 回复: 0
打印 上一主题 下一主题

Eclipse3.4.1开发基于Spring2.5+Hibernate3+Struts2项目(二) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-21 10:20 |只看该作者 |倒序浏览

续:
上面完成了Hibernate POJO对象的编码,下面开始介绍Struts2的Action的编写,Action编写和我们以前将的Struts2的Action编写没有什么两样;但这次我在这里在说明一下,Action的编码思路;
以往我们都是覆盖execute方法,实际上Action中可以写自定义的方法,以完成不同的业务逻辑;如下图我在RegisterAction中写了4个不同的业务方法;


以往我们在配置Action时,是一个Action类对应一个Action请求,其实我们可以使用这个Action类配置出多个Action请求了,不同的Action请求有不同的名子(以业务逻辑命名),这样就可以省去写type之类的参数来判断请求的业务类型,然后在一个execute方法中用if else判断执行业务逻辑块了,现在只需要按业务逻辑进行划分,不同的方法执行不同的业务处理方法,代码清楚,逻辑清楚;
我们定义的Action方法返回的Result字符串内容往往是不可读懂的,其实我们完全可以用业务逻辑名称和返回页面名定义Result字符串,或使用业务逻辑描述,不怕长。
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>

    package name="tutorial" namespace="" extends="struts-default">
        
        用户登录  -->
        action name="login"  class="loginAction"  method="login">
            result>/jsp/login_ok.jspresult>
            result name="error">/jsp/login.jspresult>
        action>
        
        用户注册  -->
        action name="register"  class="registerAction"  method="register" >
             result>/jsp/register_ok.jspresult>
             result name="error">/jsp/register.jspresult>   
        action>
        
        用户列表  -->
        action name="user_list"
                class="registerAction"
                method="list" >
             result>/jsp/user_list.jspresult>   
        action>
        
        更新用户  -->
        action name="user_update"
                class="registerAction"
                method="update" >
             result>/jsp/user_list.jspresult>   
        action>        

        删除用户  -->
        action name="user_delete"
                class="registerAction"
                method="delete" >
             result>/jsp/user_list.jspresult>   
        action>
        

        
    package>
struts>

完成了Action的编码和配置后,下面我们将继续编写DAO接口的实现,并着重讲解如何将Spring2.5的整合过程,Spring架构在项目中的作用和意义,尽管这个示例工程非常简单。关于在项目中使用Spring架构的意义,我在这里不再过多的发言,请各位查阅参考一些资料吧。

我们从Web程序的运行出发,说明每一个类的调用关系;先看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>MyTutorialdisplay-name>
    welcome-file-list>
      welcome-file>index.jspwelcome-file>
    welcome-file-list>
  
    加载struts2核心 -->
    filter>
        filter-name>struts2filter-name>
        filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        filter-class>
    filter>
    filter-mapping>
        filter-name>struts2filter-name>
        url-pattern>/*url-pattern>
    filter-mapping>

    指明spring配置文件在何处 -->
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>WEB-INF/ApplicationContext.xmlparam-value>
    context-param>

    加载spring配置文件applicationContext.xml -->
    listener>
        listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>  
  
web-app>

可以看出Struts2部分没有什么变化,依然是将站点的所有请求都转给FilterDispatcher执行完成Struts2的流程;后边有一个listener监听器,我们知道监听器在Web应用启动或重新部署时会自动运行,那么这里一定启动了Spring的ContextLoaderLister这个东西,是的这个东西用来完成解析Spring的配置,Spring的配置在上面指定了路径,WEB-INF/ApplicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   
   这里定义了SQL Server数据源  -->
   bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
         property name="driverClassName"
                   value="net.sourceforge.jtds.jdbc.Driver" >property>
         property name="url"
                   value="jdbc:jtds:sqlserver://127.0.0.1:1433/tutorial" >property>
         property name="username"  value="sa" />
          property name="password" value="sasa" />
   bean>
   
   这里定义了hibernate要使用Session工程对象,
       sessionFactory 要求IoC注入一个数据源对象,并配置了ORM映射(数据库表和对象之间)
                  配置了Hibernate的一些配置属性,如使用的SQL方言
     -->
   bean id="sessionFactory"
         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
         destroy-method="destroy" >
         property name="dataSource" ref="dataSource" />
         property name="mappingResources">
           list>
             value>com/snsoft/tutorial/domain/TUser.hbm.xmlvalue>
           list>
         property>
         property name="hibernateProperties">
           props>
             prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialectprop>
           props>
         property>         
   bean>
   
   用户DAO对象定义,也用写成 Service对象的  -->
   bean id="userDao" class="com.snsoft.tutorial.dao.UserDAOImpl" >
       property name="sessionFactory" ref="sessionFactory" />
   bean>
   
   用户登录ActionBean的定义  -->
   bean id="loginAction" class="com.snsoft.tutorial.action.LoginAction">
       property name="userDao" ref="userDao" />
   bean>
   
   用户注册ActionBean的定义  -->
   bean id="registerAction" class="com.snsoft.tutorial.action.RegisterAction">
       property name="userDao" ref="userDao" />
   bean>
   
beans>

这个文件中定义了dataSource访问SQL Server的数据源,Hibernate的Session工长,Struts2中的Action要使用Bean对象,操作数据库的DAO对象。
使用Spring整合和就是要用Spring的IoC机制使耦合关系紧密对象之间变得松散,我们可以看到项目中的Bean对象全部交由Spring管理了。我们在项目代码中无法找到HibernateTemplate(封装了Hibernate的一些操作)是从那里得到SessionFactory对象,也无法找到Action中从那里得到DAO来完成数据库操作,其实这里都是由Spring架构通过IoC完成的,通过setSessionFactory方法,为DAOImpl注入了SessionFactory对象,通过setUserDao方法为Action注入了DAO对象。
另外,我们回过头看看Action配置文件,
action name="register"  class="registerAction"  method="register" >
原本class 需要一个带有Package的全名,可这里只写了一个registerAction,然而找遍整个项目也无法找到registerAction类,只要在Spring的配置文件中有一个名为registerAction的Bean定义,没错这正式Struts2与Spring的整合模式;Struts2将Bean交由Spring管理,自己不再负责创建Bean对象,而通过Spring注入(具体工作原理可参考struts2-spring-plugin),当然要这要整合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.enable.DynamicMethodInvocation" value="false" />
    constant name="struts.devMode" value="false" />
   
    设置Struts2的Bean工厂使用Spring IoC注入  -->
    constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
   
    include file="tutorial.xml"/>
   

struts>


最后说一下项目用要到Jar库,运行项目需要用到如下Jar包,我放到VSS数据库中;


最后说一下项目发布问题,代码开发、单元测试(使用Jutil进行单元测试)后,要将项目的XML配置文件,classes文件进行发布,我们用Jar包方式发布,把我们项目中的classes打近一个tutorial-0.1.jar包中,然后再将jar包放到WEB-INF/lib目录下。

       从Eclipse3.4版本已经集成了Fat Jar的一些功能,使用File-Export选择Java-Jar打包,过程和Fat Jar打包过程类似;



最后,部署到Tomcat上跑一下吧,看看你的项目是否正常运行。祝你好运!!!



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45779/showart_1434964.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP