WebWork2与SpringFramework集成之实例篇(原创)(2)

类别:Java 点击:0 评论:0 推荐:

2WebWork2SpringFramework集成的配置

1ww2/WEB-INF/applicationContext.xml

l         由于不使用Spring MVC Framework实现Web表示层,将业务逻辑层和数据持久层组件的Bean配置从springapp-servlet.xml中移到applicationContext.xml中

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>

      <property name="url">

        <value>jdbc:mysql://localhost/test</value>

      </property>

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

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

    </bean>

 

    <bean id="prodManDao" class="db.ProductManagerDaoJdbc">

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

 

    <bean id="prodMan" class="bus.ProductManager">

        <property name="productManagerDao">

            <ref bean="prodManDao"/>

        </property>

    </bean>

 

</beans>

2ww2/WEB-INF/web.xml

l         在web.xml中增加2个context监听器

       <listener>

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

       </listener>

       <listener>

              <listener-class>com.atlassian.xwork.ext.ResolverSetupServletContextListener</listener-class>

       </listener>

3ww2/WEB-INF/classes/xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">

 

<xwork>

 

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

 

    <package name="default" extends="webwork-default"

        externalReferenceResolver="com.atlassian.xwork.ext.SpringServletContextReferenceResolver">

 

        <interceptors>

            <interceptor name="reference-resolver"

                class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor"/>

            <interceptor-stack name="externalStack">

                <interceptor-ref name="reference-resolver"/>

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

            <interceptor-stack name="externalValidationWorkflowStack">

                <interceptor-ref name="reference-resolver"/>

                       <interceptor-ref name="validationWorkflowStack" />

            </interceptor-stack>

        </interceptors>

 

        <default-interceptor-ref name="externalStack"/>

 

        <action name="springapp"

            class="web.SpringappAction">

            <external-ref name="prodMan">prodMan</external-ref>

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

                <param name="location">/WEB-INF/template/hello.vm</param>

            </result>

<!--

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

                <param name="location">/WEB-INF/jsp/hello.jsp</param>

            </result>

-->            

        </action>

 

        <action name="priceIncreaseForm"

            class="web.PriceIncreaseForm">

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

                <param name="location">/WEB-INF/template/priceincrease.vm</param>

            </result>

<!--

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

                <param name="location">/WEB-INF/jsp/priceincrease.jsp</param>

            </result>

-->            

        </action>

 

        <action name="priceIncrease"

            class="web.PriceIncreaseFormAction">

            <external-ref name="prodMan">prodMan</external-ref>

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

                <param name="location">/WEB-INF/template/priceincrease.vm</param>

            </result>

<!--

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

                <param name="location">/WEB-INF/jsp/priceincrease.jsp</param>

            </result>

-->            

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

                       <param name="actionName">springapp</param>

                </result>

                <interceptor-ref name="externalValidationWorkflowStack" />

        </action>

 

    </package>

 

</xwork>

l         XWork使用外部引用来使用Spring Bean配置中的引用,这需要:

l         在<package>标记中指定外部引用解析器为com.atlassian.xwork.ext.SpringServletContextReferenceResolver

l         重新组合Interceptor堆栈,以包含com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor;这里定义了两个Interceptor堆栈:externalStack和externalValidationWorkflowStack,前者被定义为缺省Interceptor引用,后者被需要数据有效性验证的Action引用

l         最后,使用<external-ref>标记在Action中引用外部的Spring Bean(有关Action的配置会在后面详述)

本文地址:http://com.8s8s.com/it/it16031.htm