Monday, September 18, 2017

Strut 2 and Spring Integration

To integrate Struts 2 and Spring, get and include the “struts2-spring-plugin-xxx.jar” library into your project classpath.

web.xml for the Struts-Spring integration as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
       
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>

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

</web-app>

The important thing to note here is the listener that we have configured. The ContextLoaderListener is required to load the spring context file. Spring's configuration file is called applicationContext.xml file and it must be placed at the same level as the web.xml file
  
struts.xml

<struts>
<include file="/application/party/configfiles/login.xml"/>
   <package name="default" extends="struts-default-btsl">
   <default-interceptor-ref name="defaultStack" />
   </package>
</struts>

login.xml

<struts>

<package name="login" namespace="/login" extends="struts-default-btsl">
    <action name="login_*" method="{1}" class="loginAction">
            <interceptor-ref name="defaultStack">
                        <param name="securitymanager.exclude">Y</param>
            </interceptor-ref>
            <result>/mjsp/login/tangohomepage.jsp</result>
            <result name="input">/mjsp/login/login.jsp</result>
            <result name="error">/mjsp/login/login.jsp</result>
            <result name="logout">/mjsp/login/logout.jsp</result>
            <result name="securityLogout">/defaultErrorPage2.jsp</result>
            <result name="homepage">/mjsp/login/homepage.jsp</result>
    </action>
</package>   
</struts>

applicationContext.xml


<bean id="loginActionFT" class="com.btsl.login.FT.LoginActionFT"
           parent="loginAction" autowire="default" scope="session">
           <property name="channelDAO" ref="channelDAO" />
           <property name="FTDAOI" ref="FTDAOI" />
</bean>

No comments:

Post a Comment