struts怎么找到spring的bean去处理业务

1、首先,web.xml里面需要定义struts过滤器和spring的初始化。

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>
    </filter-mapping>
<!—配置struts2.x的filter--> 
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       <!-- 
        default: /WEB-INF/ApplicationContext.xml
         -->
    </listener>
<!—- 配置listener -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:beans.xml</param-value>
       </context-param>

2、struts的配置文件中,说明action收到之后,让谁去处理,我们一般让spring处理,所以配置如下:

<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

或者

struts.objectFactory=spring

3、配置action的时候,标签class指令处理类,

a、可以自定义,比如class="ee",如果自定义,spring的配置文件,action的bean id必须也是ee,否则无法找到。
b、用xx.xx.ee(包全路劲)。这种方式spring的bean无需一样,估计是通过ee的class类型取寻找对应的bean的。

猜你喜欢

转载自smallbee.iteye.com/blog/1157903