1

我们使用 Spring IoC 和 Struts 作为 Web 应用程序框架。表示层中有一个助手类,它从请求中读取一些内容并将一些数据填充为请求属性,以供操作和其他类使用。所以我们需要这个类中的请求对象(HttpServletRequest),为此我们使用 ServletActionContext.getRequest()。

助手类是请求范围内的 Spring bean,并为所有请求调用。数据加载发生在助手的 Spring bean 中配置为“init-method”的方法中。这些动作也是 Spring bean。

在 struts.xml 中,我们通过名称引用 spring 中配置的动作 bean。例如:

<action name="myAction" class="actionBeanNameFromSpring">
    <result>myResult.jsp</result>
</action>

在这种情况下,帮助程序中的 ServletActionContext.getRequest() 方法返回正确的请求对象。然而,我们在 XML 中有其他动作,我们直接引用动作类而不是动作 bean,即我们绕过 Spring。在这些情况下,请求显示为空。

现在我知道解决方案是更改 XML 中的所有操作条目以引用 Spring bean。但我想知道为什么会发生这种情况。AFAIK,ServletActionContext 是 Struts 的东西,完全由 Struts 处理。通过 Spring(即使用 Spring 动作 bean)如何填充上下文,而不通过 Spring(即使用 Action 类名)不会?

@罗马

我假设整个内容是不必要的。以下是相关部分:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    ....

    <!-- the helper which accesses request -->
    <bean id="myActionHelper" class="com.company.product.webapp.package.MyActionHelper" init-method="init" scope="request">
        <property name="someSvcBean" ref="someSvcBean" />
    </bean>

    ....

    <!-- a typical action -->
    <bean id="myAction" class="com.company.product.webapp.action.MyAction" scope="request">
    </bean>

    ....

</beans>
4

0 回答 0