Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

spring mvc - Web flow add model attribute for binding with form values

I'm new to web flow, so I'm not sure how to do this: I have a spring mvc controller, and I have a spring webflow. One of the flow states is supposed to display a form for adding a new client. In the controller I have methods for saving the Client object (binded to the form as modelAttribute = "client") to the database. I don't know how to pass the control from the webflow to the controller and back or create a model Attribute directly in the flow for binding with the form (but as I understand later I'd still have to redirect to the controller).

Please help me, what's the best logic (solution) in this situation ?

I tried:

<view-state id="clientOptions" view="options" model="client">
    <transition on="submit" to="confirm"/>
    <transition on="back" to="viewCart"/>

my form is in the options view and the binding goes like this

<form:form modelAttribute="client" method="post">
See Question&Answers more detail:os

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming you have a mapping in your web.xml as:


Define the following in your servlet xml file (per above web.xml definition yourApp-Servlet.xml):

    <!--Handler mapping for your controller which does some work per your requirements-->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="order" value="1" />
        <property name="mappings">
                <prop key="/handledByController.htm">yourController</prop>
    <!--Your controller bean definition-->
    <bean id="yourController" class="package.YourController">
        //some properties like service reference
    <!--Class which handles the flow related actions-->
    <bean id="classForThisFlow" class="package.ClassForThisFlow">

I assume you have web flow required configuration defined like below (for webflow 2.3 - for 1 configuration is little different) in webflowconfig xml:

    <!--View  resolvers-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />

    <bean id="flowViewResolver" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">  
        <property name="viewResolvers" ref="internalResourceViewResolver"/>  
    <!--flow xml registration - flow xmls are in flows folder-->
    <flow:registry id="flowRegistry">
        <flow:location path="/WEB-INF/flows/**/*-flow.xml" />

    <!--Flow controller to which dispacther servlet delegates the control to manage flow execution-->
    <bean name="flowController" class="org.springframework.webflow.executor.mvc.FlowController">
        <property name="flowExecutor" ref="flowExecutor" />

    <flow:executor id="flowExecutor" registry-ref="flowRegistry">
        <flow:repository type="continuation" max-conversations="1" max-continuations="30" />
            <flow:listener ref="listener"/>
    <!--validator to be identified and registered for view state validations if enabled-->
    <bean name="validator" class="org.springframework.validation.localvalidation.LocalValidatorFactoryBean"/>  
    <flow:flow-builder-services id="flowBuilderServices" view-factory-creator="flowViewResolver" validator="validator"/>  

In your flow xml you have view as:

            <flow xmlns="http://www.springframework.org/schema/webflow"
<action-state id="prepareFlow">
    <evaluate expression="classForThisFlow.prepareFlowForm()" result="flowScope.client"/>
    <transition to="clientOptions" />
    <view-state id="clientOptions" view="options" model="client">
        <!--If you have defined validator for the action class handling this view, then validation will kick in. You can disable validation by validate="false"-->
                        <!--By default binding of form fields will happen to the model specified; you can disable it by bind="false"--> 
        <transition on="submit" to="confirm"/>
        <transition on="back" to="viewCart"/>

    <action-state id="confirm">
        <!--Do some action on the form-->
        <evaluate expression="classForThisFlow.methodName(flowRequestContext,flowScope.client)"/>
        <transition to="returnToController" />

    <!--This will take the control back to yourController; See mappingis specified as *.htm in web.xml-->
    <end-state id="returnToController" view="externalRedirect:handledByController.htm" /> 

You can pass client to controller through session as:

    public class ClassForThisFlow{
      public Client prepareFlowForm(){
        Client client= new Client();
    return client;
       public void methodName(RequestContext context, Client client){
    HttpSession session = ((HttpServletRequest)context.getExternalContext().getNativeRequest()).getSession();

This way you can navigate through your flow and on submit "confirm", you will be taken to action class, if needed, else change both end-state id and transition on submit to to "confirm", which will direct the control to yourController. In your controller you can access session and retieve the client object for further processing.

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share