Spring与Struts 1集成 - 向ActionForm注入依赖项

时间:2009-11-08 12:37:29

标签: java spring spring-mvc struts

我们正在扩展旧的Struts 1项目以进行Spring管理 - 但我们希望尽可能少地更改代码和流程。因此,我们决定让Struts管理请求,并通过org.springframework.web.struts.DelegatingActionProxy类委派行动。

示例:

struts-config.xml包含:

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

action-servlet.xml包含:

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

现在情节变浓了

在applicationContext.xml中定义了正常的Spring bean。一个例子:

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

我现在希望将parserUtils bean连接到 ActionForm (不是自动 - 但这不是问题)。

如果我想将其连接到 Action ,我只需在action-servlet.xml中定义以下内容:

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

其中以下是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>

但是stuts-config还包含以下 ActionForm 定义:

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

我希望将parserUtils bean连接到CaseUpdateForm类。

我该怎么办?

全部谢谢!

2 个答案:

答案 0 :(得分:0)

您始终可以选择以编程方式从Spring上下文中获取parserFactory bean。

Spring的DispatcherServlet“将servlet的应用程序上下文发布为会话属性。您可以从那里检索应用程序上下文,并从该上下文手动获取parserFactory

让你入门的东西:

public Object getBean(ServletContext servletContext, String servletName, String beanName) {
    String attrName = FrameworkServlet.SERVLET_CONTEXT_PREFIX + servletName;
    BeanFactory beanFactory = (BeanFactory) servletContext.getAttribute(attrName);
    return beanFactory.getBean(beanName);
}

如您所见,您需要访问ServletContext对象(您的ActionForm应该能够使用getServlet().getServletContext()获得),以及Spring servlet的名称(如定义的那样)在web.xml中),以及您需要检索的Spring应用程序上下文中的bean的名称。

这很丑陋,但如果您可以访问上述信息,它应该可以使用。

答案 1 :(得分:0)

好像我找到了一个解决方案:

在struts-config.xml文件中我改变了:

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

为:

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

我已将以下内容添加到action-servlet.xml文件中:

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

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

我已将以下内容添加到CaseUpdateForm.java类文件中:

private ParserUtils parserUtils;

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

以下是CaseUpdateAction.java类文件:

private CaseUpdateForm caseUpdateForm;

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

最后方法:

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

插入了以下代码行:

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

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

我还必须在类路径中包含以下jar:

  

弹簧webmvc.jar

这是由于使用了类:ServletRequestDataBinder

相关问题