Spring 与 Struts 1 集成 - 将依赖项注入到 ActionForm
我们正在扩展一个旧的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎我找到了解决方案:
在 struts-config.xml 文件中,我已将:更改
为:
我已将以下内容添加到 action-servlet.xml 文件中:
并且:
我已将以下内容添加到 CaseUpdateForm.java类文件:
以及 CaseUpdateAction.java 类文件的以下内容:
最后的方法:
插入以下代码行:
我还必须在类路径中包含以下 jar:
这是由于使用了该类:
ServletRequestDataBinder
It seems as if I have found a solution:
In the struts-config.xml file I have changed:
to:
I have added the following to the action-servlet.xml file:
And:
I have added the following to the CaseUpdateForm.java class file:
And the following to the CaseUpdateAction.java class file:
Finally the method:
got the following lines of code inserted to it:
I also had to include the following jar in the classpath:
That is due to the usage of the class:
ServletRequestDataBinder
您始终可以选择以编程方式从 Spring 上下文中获取
parserFactory
bean。Spring 的 DispatcherServlet 将 servlet 的应用程序上下文作为会话属性“发布”。您可以从那里检索应用程序上下文,并从该上下文手动获取您的
parserFactory
。帮助您入门的东西:
如您所见,您需要访问
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 yourparserFactory
from that context.Something to get you started:
As you can see, you need access to the
ServletContext
object (which yourActionForm
should be able to get usinggetServlet().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.