龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

Webwork 和 spring 的整合

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
1 适合读者 本文针对有一定的web基础,webwork基础,对spring有一定的了解。 http://www.springframework.org 站点可以了解更多关于spring的具体信息. Spring是一个很好的AOP框架,能提供自动的事务

  
  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
  
  

精彩图集

赞助商链接