1
votes

I'm using Spring MVC + Spring Webflow 2.

I would like to define a @Bean for an action-state, but i don't know how to do this in Java Annotation, as i get this error:

Method call: Method execute() cannot be found on com.myapp.action.GaraAgenziaAction type

here an example of what i want to do: spring-webflow-no-actions-were-executed

My Bean:

import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

public class GaraAgenziaAction implements Action {

    @Override
    public Event execute(RequestContext rc) throws Exception {                
        return new Event(this, "success");
    }
}

Flow XML:

<transition on="fail" to="gara-agenzie"/>

<transition on="success" to="gara-conferma"/>

My webAppConfig:

@Bean
public Action GaraAgenziaAction()
{
    GaraAgenziaAction garaAgenziaAction = new GaraAgenziaAction();

    return garaAgenziaAction;

}

Thank you very much



UPDATE resolved thanks to @Prasad suggestions:

My Bean (added @Component):

import org.springframework.stereotype.Component;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

@Component
public class GaraAgenziaAction implements Action {

    @Override
    public Event execute(RequestContext rc) throws Exception {                
        return new Event(this, "success");
    }
}

My webAppConfig (changed name of the bean with lowercase):

@Bean
public Action garaAgenziaAction()
{
    GaraAgenziaAction beanAction = new GaraAgenziaAction();

    return beanAction;

}

Flow XMl configuration (changed bean name to lowercase and pass flowRequestContext as parameter):

<action-state id="action-agenzie">
    <evaluate expression="garaAgenziaAction.execute(flowRequestContext)"></evaluate>        

    <transition on="fail" to="gara-agenzie"/>

    <transition on="success" to="gara-conferma"/>
</action-state>

Now it's working fine!

1
Just define GaraAgenziaAction bean in 1) xml bean definitions file if xml is used or 2) use Component annotation on the action and call this in action-state. Also did you observe, you are using same class name for the one implementing interface and the one with Bean annotation? - Prasad
Check the example mentioned in this post:stackoverflow.com/questions/23342621/…, which mostly covers the configuration and access the methods in ClassForThisFlow class in action-state(for SWF 1)/evaluate-action(for SWF 2 and later). Though ClassForThisFlow does not implement action. You can define any method in ClassForThisFlow and access it. - Prasad

1 Answers

1
votes

Define the action class in your servlet xml file as:

    <!--Class which handles the flow related actions-->
    <bean id="garaAgenziaAction" class=" com.myapp.action.GaraAgenziaAction">
    </bean>

or annotate it with Component as:

    @Component
    public class GaraAgenziaAction implements Action{
            @Override
            public Event execute(RequestContext rc) throws Exception {                
            return new Event(this, "success");
            }
    }

In your flow xml access it as:

    <action-state id="action-agenzie">
        <evaluate expression="garaAgenziaAction.execute(flowRequestContext)"></evaluate>
        <transition on="fail" to="gara-agenzie"/>
        <transition on="success" to="gara-conferma"/>
    </action-state>

For configuration details you can find it in the answer in this link.