Java SpringWebFlow-无法对模型bean调用注释验证
请参阅下面的代码片段。我的流程从显示一个包含3个字段的简单JSP开始。当我提交表单时,我希望通过表单bean上的注释配置的验证JSR-303能够启动并显示错误消息。但事实并非如此。页面将被提交到searchActions.findExistingPlayer方法。任何指示都会有帮助 配置文件:Java SpringWebFlow-无法对模型bean调用注释验证,java,spring,validation,spring-webflow,spring-webflow-2,Java,Spring,Validation,Spring Webflow,Spring Webflow 2,请参阅下面的代码片段。我的流程从显示一个包含3个字段的简单JSP开始。当我提交表单时,我希望通过表单bean上的注释配置的验证JSR-303能够启动并显示错误消息。但事实并非如此。页面将被提交到searchActions.findExistingPlayer方法。任何指示都会有帮助 配置文件: <?xml version="1.0" encoding="UTF-8"?> <!-- Source project: sip05, branch: 03 (Maven Project)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Source project: sip05, branch: 03 (Maven Project) -->
<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.0.xsd"
start-state="findExistingPlayerForm">
<view-state id="findExistingPlayerForm">
<on-render>
<evaluate expression="findExistingPlayerFormAction.setupForm"></evaluate>
</on-render>
<transition on="find" to="findExistingPlayerFormActionState">
<evaluate expression="findExistingPlayerFormAction.bindAndValidate"></evaluate>
</transition>
</view-state>
<action-state id="findExistingPlayerFormActionState">
<evaluate expression="searchActions.findExistingPlayer"></evaluate>
<transition on="success" to="displayFindExistingPlayerResult"></transition>
</action-state>
<view-state id="displayFindExistingPlayerResult">
<<---Some transitions-->>>
</view-state>
<end-state id="newSearchEndState" />
</flow>
Web流上下文:
<flow:flow-builder-services id="flowBuilderServices"
development="true"
validator="validator"/>
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
Flow.xml
<var name="playerSearchForm" class="com.saurabhd.springwebflow.form.PlayerSearchForm"/>
<view-state id="findExistingPlayerForm" model="playerSearchForm">
<transition on="find" to="findExistingPlayerFormActionState">
</transition>
</view-state>
我注意到您没有在视图状态上正确定义模型。定义模型时,必须将正在使用的模型的实例设置为流范围或视图范围 您正在做的是在流中创建PlayerSearchForm的新实例,并绑定到视图状态。这不起作用,因为您必须绑定正在视图上工作的实例 因此,有两种方法可以传递对象的引用 第一种方式:
//You can set objects into the flow scope, like this.
RequestContextHolder.getRequestContext().getFlowScope().put("playerSearchForm", instanceOfPlayerSearchForm);
第二种方式:
<evaluate expression="findExistingPlayerFormAction.getPlayerSearchForm()" result="flowScope.playerSearchForm"/>
public PlayerSearchForm getPlayerSearchForm(){
return instanceOfPlayerSearchForm;
}
伙计们,有人能在这里帮忙并提供建议吗?@Willieweeler,你们能在这里帮忙吗。
public void validateFindExistingPlayerForm(ValidationContext context){
MessageContext messages = context.getMessageContext();
if(StringUtils.isEmpty(firstName)){
messages.addMessage(new MessageBuilder().error().source("firstName").
defaultText("Please enter the value for First Name.").build());
}
}
<var name="playerSearchForm" class="com.saurabhd.springwebflow.form.PlayerSearchForm"/>
<view-state id="findExistingPlayerForm" model="playerSearchForm">
<transition on="find" to="findExistingPlayerFormActionState">
</transition>
</view-state>
//You can set objects into the flow scope, like this.
RequestContextHolder.getRequestContext().getFlowScope().put("playerSearchForm", instanceOfPlayerSearchForm);
<evaluate expression="findExistingPlayerFormAction.getPlayerSearchForm()" result="flowScope.playerSearchForm"/>
public PlayerSearchForm getPlayerSearchForm(){
return instanceOfPlayerSearchForm;
}