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

发布于 2024-08-10 05:33:11 字数 1806 浏览 1 评论 0原文

我们正在扩展一个旧的 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" />

现在情节变得更加丰富

普通 Spring beans 已在 applicationContext.xml 中定义。一个例子:

<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 类。

我必须做什么?

谢谢大家!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2024-08-17 05:33:11

似乎我找到了解决方案:

在 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:

spring-webmvc.jar

这是由于使用了该类:ServletRequestDataBinder

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

忱杏 2024-08-17 05:33:11

您始终可以选择以编程方式从 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 的名称。

它非常丑陋,但如果您能够访问上述信息,它应该可以工作。

You always have the option of programmatically fetching the parserFactory bean from the Spring context.

Spring's DispatcherServlet "publishes" the servlet's application context as a session attribute. You can retrieve the application context from there, and manually obtain your parserFactory from that context.

Something to get you started:

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);
}

As you can see, you need access to the ServletContext object (which your ActionForm should be able to get using getServlet().getServletContext()), plus the name of the Spring servlet (as defined in web.xml), plus the name of the bean in the Spring application context that you need to retrieve.

It's pretty ugly, but it should work, if you have access to the above information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文