WEB2.0开发

心态决定行为,行为决定习惯,习惯决定命运

Webwork 和 spring 的整合




1 适合读者
本文针对有一定的web基础,webwork基础,对spring有一定的了解。

http://www.springframework.org/ 站点可以了解更多关于spring的详细信息. Spring是一个很好的AOP框架,能提供自动的事务管理。

http://www.opensymphony.com/webwork/ 站点可以了解更多的webwork . Webwork是一个很好的 MVC 框架,以下简单介绍,webwork 和 spring 的融合,以用户注册为例。

2 整合步骤
2 .1使用 SpringObjectFactory
dev.java.net上的 xwork-optional 包括了对 xwork-spring 的支持,可以下载些包。包中只有4个类,可以根据具体情况使用。我在例子中使用了SpringObjectFactory类和SpringObjectFactoryListener 类,并安照webwork的文件在web.xml加入了,以下节点

<!-- This needs to be after Spring ContextLoaderListener -->

<listener>

<listener-class>com.opensymphony.xwork.spring.SpringObjectFactoryListener</listener-class>

</listener>

但在实际工作中不能使用,回为在SpringObjectFactoryListener类中写的,加载些类必须要先加载org.springframework.web.context.ContextLoaderListener类,由于些类在web.xml配置如下,后于listener的执行。

<servlet>

<servlet-name>spring</servlet-name>

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

<load-on-startup>0</load-on-startup>

</servlet>

ContextLoaderServlet 的作用是加载 spring配置信息(在int方法中),SpringObjectFactoryListener 的作用是设置 XWork 和 Webwork 的环境,使这能从spring 中加载数据和信息(在contextInitialized(ServletContextEvent event)方法中,当启动web应用程序是调用).

要使ContextLoaderServlet比SpringObjectFactoryListener类更早执行,我使用的方法是重载ContextLoaderServlet,在中子类的int方法中设置XWork 和 Webwork 的环境,去掉SpringObjectFactoryListener 监听器,并整改web.xml中的spring配置,如下:

<servlet>

<servlet-name>spring</servlet-name>

<servlet-class>spring.server.ContextLoaderServletServer</servlet-class>

<load-on-startup>0</load-on-startup>

</servlet>

2 .2 配置webwork
在webwork.xml中增加如下节点

<action name="registerSupport-spring" class="registerSupportBean" >

<result name="success" type="dispatcher">

<param name="location">/register-result.jsp</param>

</result>

<result name="input" type="dispatcher">

<param name="location">/registerSupport.jsp</param>

</result>

<interceptor-ref name="validationWorkflowStack"/>

</action>

其中的registerSupportBean在spring中配置

2 .3 配置spring
在spring中加入action 配置如下

<!-- action -->

<bean name="registerSupportBean" class="spring.action.RegisterActionSupport"

singleton="false" >

<property name="userBean">

<ref bean="userBean"/>

</property>

</bean>

些处的registerSupportBean 即为在webwork中要调用的类名。

Copyright 2003-2005 www.sohozu.com All Rights Reserved

posted on 2006-04-05 12:35 ymx 阅读(251) 评论(0)  编辑  收藏 所属分类: WebWorkSpring


只有注册用户登录后才能发表评论。


网站导航: