Ousmane MBINTE
Ousmane MBINTE

Reputation: 742

how to delegate struts2 action's instantiation to spring using annotations

Hello I have a Struts2 application where struts actions were defined in the struts.xml file. Now, I want to delegate this to Spring in order to have actions with session scope.

How can I do this using annotations? Because it will be too long to redefine all the services in the applicationContext.xml

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="false" />
    <constant name="struts.action.extension" value="do" />
    <constant name="struts.custom.i18n.resources" value="com.omb.i18n.StrutsResourceBundle" />
    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> 
    <constant name="struts.objectFactory.spring.autoWire" value="name" />
    <constant name="struts.i18n.encoding" value="ISO-8859-1" />
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.configuration.xml.reload" value="false" />
    <constant name="struts.locale" value="fr" />
    <constant name="struts.multipart.maxSize" value="100000000000" />
    <constant name="struts.enable.SlashesInActionNames" value="true" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

    <constant name="struts.codebehind.classSuffix" value="Controller"/>
    <constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
    <constant name="struts.codebehind.action.checkAnnotation" value="false"/>
    <constant name="struts.codebehind.action.defaultMethodName" value="index"/>
    <constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />

    <package name="default" extends="tiles-default" namespace="/">

        <interceptors>

            <interceptor name="params-filter"
                class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />

            <interceptor-stack name="defaultStack">
                <interceptor-ref name="exception" />
                <interceptor-ref name="alias" />
                <interceptor-ref name="servletConfig" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="chain" />
                <interceptor-ref name="modelDriven" />
                <interceptor-ref name="fileUpload">
                    <param name="maximumSize">11204928</param>
                </interceptor-ref>
                <interceptor-ref name="staticParams" />
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="params" />
                <interceptor-ref name="prepare" />
                <interceptor-ref name="validation" />
                <interceptor-ref name="workflow" />
            </interceptor-stack>

        </interceptors>

        <default-interceptor-ref name="defaultStack" />

        <global-results>
            <result name="technicalError" type="chain">
                errorAction
            </result>
            <result name="sessionInvalidError" type="tiles">
                sessionInvalid
            </result>
            <result name="blank" type="tiles">blank</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="technicalError" />
            <exception-mapping
                exception="com.omb.service.exception.UserSessionInvalidException"
                result="sessionInvalidError" />

        </global-exception-mappings>

    </package>

    <package name="omb" extends="default" namespace="/omb">
        <action name="doAction.do" class="myAction" method="{1}">
            <result name="success" type="redirectAction">
                <param name="namespace">/omb</param>
                <param name="actionName">displayResult</param>
            </result>
            <result name="error" type="redirectAction">
                <param name="namespace">/error</param>
                <param name="actionName">displayError</param>
            </result>
        </action>
    </package>
</struts>

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <display-name>MyApplication</display-name>
    <description>Application OMB</description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml,
            classpath:applicationContext-web.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>org.apache.tiles.DEFINITIONS_CONFIG</param-name>
        <param-value>/WEB-INF/tiles/tiles-defs.xml</param-value>
    </context-param>

    <context-param>
        <param-name>struts.ajax.debug</param-name>
        <param-value>${struts.ajax.debug}</param-value>
    </context-param>

    <filter>
        <filter-name>Acegi Filter Chain Proxy</filter-name>
        <filter-class>
            org.acegisecurity.util.FilterToBeanProxy
        </filter-class>
        <init-param>
            <param-name>targetClass</param-name>
            <param-value>
                org.acegisecurity.util.FilterChainProxy
            </param-value>
        </init-param>
    </filter>

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

    <filter-mapping>
        <filter-name>Acegi Filter Chain Proxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Java Melody filter mapping -->   
    <filter-mapping>     
          <filter-name>monitoring</filter-name>     
          <url-pattern>/*</url-pattern>   
    </filter-mapping>

    <listener>
        <listener-class>
            com.omb.listener.ApplicationListener
        </listener-class>
    </listener>

    <listener>
        <listener-class>
            org.apache.struts2.tiles.StrutsTilesListener
        </listener-class>
    </listener>

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

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

MyAction.java:

package com.omb.actions;

@Repository("myAction")
@Scope("session")
public class MyAction extends ActionSupport {

    @Qualifier("myService")
    private MyService myService;

    public String execute() throws Exception {
        myService.callMethode();
        return SUCCESS;
    }


    public MyService getMyService() {
        return this.myService;
    }

    public void setMyService(MyService myService) {
        this.myService = myService;
    }
}

Upvotes: 3

Views: 1843

Answers (1)

Roman C
Roman C

Reputation: 1

If you are using Spring plugin, so the integration of struts with spring is successful the objectFactory of struts is delegating the building of the action to spring, so it could use to inject dependencies from the spring application context. But the action bean is managed by the struts container, so it keep a possibility of DI internally. If you want to completely delegate the bean to be managed by spring you should configure action beans in application context and in action configuration replace the class references to the spring ids.

In the applicationContext.xml place

<!--<context:annotation-config/>-->
<context:component-scan base-package="your.base.package"/>

to point the spring to scan for beans in the package hierarchy.

Upvotes: 2

Related Questions