SSH之struts2.0

    技术2022-05-20  71

    1:将struts2-spring-plugin-2.1.6.jar放在工程下

    2:在web.xml中配置struts2.0的过滤器和spring的监听器

     

    <!-- 配置struts2.0核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

    <!-- 配置spring bean Listener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-config/environment-context.xml, /WEB-INF/spring-config/beans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

    3:struts.xml中配置与spring关联

    在struts.xml中配置

    <constant name="struts.objectFactory" value="spring" /> <constant name="struts.devMode" value="true" />

    4:定义spring的bean

    使用控制器类的类型定义一个spring的bean,注意需要使用id而不是name声明bean

    <bean id="login" class="controller.LoginAction"> <property name="operatorService"> <ref bean="operatorService"/> </property> </bean>

    5:使用与spring中定义的bean声明struts2.0的控制器

    配置struts2.0的控制器时需要将class指定成spring中已经配置的bean,在下面的例子中loginAction类型是login,可以在spring的配置文件中对login注入其它bean。

    <!--被注释掉的控制器的声明使用struts2.0的控制器声明方法,无法与spring的bean进行关联 --> <!--action name="loginAction" class="controller.LoginAction"-->

    <!-- 使用与spring的bean关联的登陆控制器,注意class使用的是spring中声明的bean "login"--> <action name="loginAction" class="login"> <result name="success">/jsp/loginSuccess.jsp</result> <result name="error">/jsp/loginFail.jsp</result>

    </action>

     

    struts2.0与spring的集成主要是上面的步骤4、5,使用spring中配置好的bean的id为类型声明struts2.0的控制 器。集成后的系统中,控制器仍然使用struts2.0的控制器,只是此时的控制器已经处于spring的管理下,可以使用spring的依赖注入对这个 这个控制器注入其它的bean。


    最新回复(0)