Web流程添加与表单值绑定的模型属性
我是新来的网络流量,所以我不知道如何做到这一点:我有一个春天mvc控制器,并且我有一个春天webflow。 其中一个流程状态应该显示用于添加新客户端的表单。 在控制器中,我有用于将客户端对象(绑定到formAttribute =“client”的表单)保存到数据库的方法。 我不知道如何将控制权从webflow传递到控制器,或者直接在流中创建模型属性,以便与表单绑定(但我后来知道我仍然必须重定向到控制器)。
请帮助我,这种情况下最好的逻辑(解决方案)是什么?
我试过了:
<view-state id="clientOptions" view="options" model="client">
<transition on="submit" to="confirm"/>
<transition on="back" to="viewCart"/>
</view-state>
我的表单在选项视图中,绑定是这样的
<form:form modelAttribute="client" method="post">
假设你在web.xml中有一个映射:
<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):
<!--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>
我假设你有webflow所需的配置定义如下(对于Webflow 2.3 - 对于1配置有点不同)在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中,你有以下观点:
<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" />
您可以通过会话将客户端传递给控制器:
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 。 在您的控制器中,您可以访问会话并检索客户端对象以进行进一步处理。
我使用application-context.xml来声明我的控制器或处理程序,例如:
<!-- handler/controller-->
<bean name="contextHandler" class="aaaa.bbbb.ContextHandler" scope="session">
<property name="service" ref="usuarisBO" />
<property name="usuari" ref="usuariLDAP" />
</bean>
那么在我的流程中,我可以调用id对象和一些方法:
如果你需要一个交易,你可以在一个行动方法中做到这一点,例如:
<action-state id="a_pas2">
<evaluate expression="contextHandler.carreguemValorsPas2(flowRequestContext,usuari)" />
<transition on="*" to="v_pas2" />
</action-state>
<view-state id="v_pas2" view="/WEB-INF/xhtml/crearCalendariFiscal/formulariCrear2.xhtml">
<transition on="pas3" to="a_pas3"/>
</view-state>
我希望它有帮助。
链接地址: http://www.djcxy.com/p/39101.html上一篇: Web flow add model attribute for binding with form values
下一篇: Spring MVC Issue