mercredi 15 juillet 2015

Struts2 chaining actions manually

I have a very unusual problem to solve and can't seem to get it right. To spare a long story what I am trying to do is to invoke a number of small actions from a one central action.

These small actions are Spring injected into a Map of String to some base class object. So I want to loop through and invoke each of these action one by one.

I do know of the "chain" results type, and I am aware of the "redirect" and "forward" results. These do not work for my case.

The use case I am trying to cover is a page with components and each component can update itself by calling the corresponding action. However, on initial page fetch I want to run all of the actions for this page to get one object with all the data on initial fetch.

To illustrate in a simplified manner here is how the code will look like:

struts.xml

<action name="*/data/*" class="action.data.{1}.{2}" >
        <result name="success" type="json">
            <param name="noCache">true</param>
            <param name="ignoreHierarchy">false</param>
            <param name="root">responseHandler.response</param>
            <param name="excludeNullProperties">true</param>
        </result>
        <result name="error" type="json">   
            <param name="noCache">true</param>
            <param name="ignoreHierarchy">false</param>
            <param name="root">responseHandler.response</param>
            <param name="excludeNullProperties">true</param>
        </result>
    </action>

applicationContext.xml

<bean id="action.data.resourceA.all" class="com.xyz.data.resourceA.PageDataAction" scope="prototype">
    <property name="dataProviders">
        <map>
            <entry key="componentA" value-ref="action.data.resourceA.componentA" />
            <entry key="componentB" value-ref="action.data.resourceA.componentB" />
        </map>
    </property>
</bean>
<bean id="action.data.resourceA.componentA" class="com.xyz.data.resourceA.ComponentAAction" scope="prototype">
</bean>
<bean id="action.data.resourceA.componentB" class="com.xyz.data.resourceA.componentBAction" scope="prototype">
</bean>

In the PageDataAction I would like to loop through the Map of the injected Actions and invoke one by one.

I have seen a few places where folks get the RequestDispatcher but for this technique you need well defined actions. I am using wildcard mapping and that would not work for me.

Another approach that I have seen is pushing the actions to the top of the ValueStack but I was not able to get this to work. Any references to working examples would save my day :)

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire