Web流添加模型属性以与表单值绑定 [英] Web flow add model attribute for binding with form values

查看:97
本文介绍了Web流添加模型属性以与表单值绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web流的新手,所以不确定如何执行此操作: 我有一个Spring MVC控制器,并且我有一个Spring Webflow.流程状态之一应该显示用于添加新客户端的表单.在控制器中,我有一些方法可以将Client对象(以modelAttribute ="client"的形式绑定到)保存到数据库中.我不知道如何将控件从Webflow传递到控制器,然后直接在流中创建或返回模型属性以与表单绑定(但是据我所知,我仍然必须重定向到控制器).

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 ?

我尝试过:

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

我的表单位于选项视图中,绑定像这样

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

<form:form modelAttribute="client" method="post">

推荐答案

假设您的web.xml中有一个映射为:

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

<servlet>
    <servlet-name>yourApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>yourApp</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

在servlet xml文件中定义以下内容(在web.xml定义yourApp-Servlet.xml上方):

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">
            <props>
                <prop key="/handledByController.htm">yourController</prop>
            </props>
        </property>
    </bean>
    <!--Your controller bean definition-->
    <bean id="yourController" class="package.YourController">
        //some properties like service reference
    </bean>
    <!--Class which handles the flow related actions-->
    <bean id="classForThisFlow" class="package.ClassForThisFlow">
    </bean>

我假设您在webflowconfig xml中定义了如下所示的Web流必需的配置(对于webflow 2.3-1种配置几乎没有什么不同):

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>

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

    <!--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" />
    </bean>

    <flow:executor id="flowExecutor" registry-ref="flowRegistry">
        <flow:repository type="continuation" max-conversations="1" max-continuations="30" />
        <flow:execution-listeners>
            <flow:listener ref="listener"/>
        </flow:execution-listeners>     
    </flow:executor>
    <!--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"/>  

在流xml中,您的查看方式为:

In your flow xml you have view as:

            <flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
    http://www.springframework.org/schema/webflow/spring-webflow-2.3.xsd"
start-state="prepareFlow">
<action-state id="prepareFlow">
    <evaluate expression="classForThisFlow.prepareFlowForm()" result="flowScope.client"/>
    <transition to="clientOptions" />
</action-state>
    <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"/>
    </view-state>

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

    <!--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();
              session.setAttribute("client",client);
       }
       .....
       }

通过这种方式,您可以浏览流程并提交确认",如果需要的话,您将进入操作类别,否则将最终状态ID和提交时的过渡更改为确认",这将指导控制到yourController.在您的控制器中,您可以访问会话并重新绑定客户端对象以进行进一步处理.

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.

这篇关于Web流添加模型属性以与表单值绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆