我是网络流量的新手,所以我不确定如何执行此操作: 我有一个弹簧mvc控制器,我有一个弹簧webflow。其中一个流状态应该显示用于添加新客户端的表单。在控制器中,我有方法将Client对象(绑定到表单作为modelAttribute =" 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">
答案 0 :(得分:3)
假设您的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>
我假设你在webflowconfig xml中有如下定义的Web流程所需配置(对于webflow 2.3 - 1配置稍有不同):
<!--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"/>
在你的flow 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和转换更改为“确认”,这将指示控制你的控制器。在您的控制器中,您可以访问会话并撤消客户端对象以进行进一步处理。
答案 1 :(得分:0)
我用来在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>
我希望它有所帮助。