Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring integration with Struts 1 - injecting dependencies to the ActionForm

We are expanding an old Struts 1 project to be Spring managed - yet we wish to change the code and the flow as little as possible. We have therefore decided to leave Struts to manage the requests and have delegated the actions via the org.springframework.web.struts.DelegatingActionProxy class.

Example:

The struts-config.xml contains:

<action name="InvalidSession" 
        path="/pages/InvalidSession"
     type="org.springframework.web.struts.DelegatingActionProxy" 
        scope="request">
    <forward name="success" path="/pages/Startup.do" />
</action>

The action-servlet.xml contains:

<bean name="/pages/InvalidSession"
      class="com.xxx.actions.InvalidSessionAction" />

Now the plot thickens:

Normal Spring beans have been defined in the applicationContext.xml. An example:

<bean id="parserUtils" class="com.xxx.el.parser.ParserUtils" >
    <property name="parserFactory" ref="parserFactory"/>
</bean>

I now wish to wire (not automatically - but that is not an issue) the parserUtils bean to an ActionForm.

Had I wanted to wire it to an Action I would simply define the follwing in the action-servlet.xml:

<bean name="/pages/CaseUpdate"
      class="com.xxx.actions.CaseUpdateAction" >
   <property name="parserUtils" ref="parserUtils" />
</bean>

where the following is in the struts-config.xml:

<action path="/pages/CaseUpdate" 
        name="CaseUpdateForm"
     type="org.springframework.web.struts.DelegatingActionProxy" 
        scope="request">
   <forward name="success" path="/pages/SwitchView.do" />
</action>

But the stuts-config also contains the following ActionForm definition:

<form-bean name="CaseUpdateForm"
           type="com.xxx.forms.CaseUpdateForm" />

and I wish to wire the parserUtils bean to the CaseUpdateForm class.

What must I do?

Thanks all!

like image 646
Yaneeve Avatar asked Nov 21 '25 14:11

Yaneeve


1 Answers

It seems as if I have found a solution:

In the struts-config.xml file I have changed:

<form-bean name="CaseUpdateForm"
           type="com.xxx.forms.CaseUpdateForm" />

to:

<form-bean name="CaseUpdateForm"
           type="org.springframework.web.struts.SpringBindingActionForm" />

I have added the following to the action-servlet.xml file:

<bean name="CaseUpdateForm" 
      class="com.xxx.forms.CaseUpdateForm" >
   <property name="parserUtils" ref="parserUtils" />
</bean>

And:

<bean name="/pages/CaseUpdate" 
      class="com.xxx.actions.CaseUpdateAction" >
   <property name="caseUpdateForm" ref="CaseUpdateForm" />
</bean>

I have added the following to the CaseUpdateForm.java class file:

private ParserUtils parserUtils;

public void setParserUtils(ParserUtils parserUtils) {
    this.parserUtils = parserUtils;
}

And the following to the CaseUpdateAction.java class file:

private CaseUpdateForm caseUpdateForm;

public void setCaseUpdateForm(CaseUpdateForm caseUpdateForm) {
    this.caseUpdateForm = caseUpdateForm;
}

Finally the method:

public ActionForward execute(ActionMapping mapping,
                     ActionForm form,
                 HttpServletRequest request,
                 HttpServletResponse response) 
                throws Exception 

got the following lines of code inserted to it:

SpringBindingActionForm springBindingActionForm = (SpringBindingActionForm) form;
ServletRequestDataBinder binder = 
    new ServletRequestDataBinder(caseUpdateForm, "CaseUpdateForm");

binder.bind(request);
springBindingActionForm.expose(binder.getBindingResult(), request);

I also had to include the following jar in the classpath:

spring-webmvc.jar

That is due to the usage of the class: ServletRequestDataBinder

like image 176
Yaneeve Avatar answered Nov 24 '25 05:11

Yaneeve