gpt4 book ai didi

java - Spring Webflow - 无法在模型 bean 上调用注释验证

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:39 25 4
gpt4 key购买 nike

请参阅下面的代码片段。我的流程从显示一个包含 3 个字段的简单 JSP 开始。当我提交表单时,我希望通过表单 bean 上的注释配置验证 (JSR-303),以启动并显示错误消息。但这并没有发生。该页面正在提交给 searchActions.findExistingPlayer 方法。任何指示都会有帮助。

配置文件:

<?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>

Action 映射

<bean id="findExistingPlayerFormAction" class="org.springframework.webflow.action.FormAction">
<property name="formObjectClass"
value="com.saurabhd.springwebflow.form.PlayerSearchForm" />
</bean>

表格:

public class PlayerSearchForm implements Serializable{
private static final long serialVersionUID = 1L;


private String firstName;

private String lastName;
private String homePhone;

@NotEmpty
@Size(min=10)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

@NotBlank
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
}

网络流上下文:

<flow:flow-builder-services id="flowBuilderServices"
development="true"
validator="validator"/>
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

JSP

<%-- Source project: sip05, branch: 03 (Maven Project) --%>
<%@ include file="/WEB-INF/jsp/taglibs.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form">

<head><title>Find Existing Player(s)</title></head>

<body>
<h2>Search</h2>
<p>
Please fill the info below:
</p>

<form:form commandName="playerSearchForm" action="${flowExecutionUrl}">
<label for="firstname">Player First Name</label>
<form:input path="firstName" /><br/>
<form:errors path="firstName"/> <br/><br/>

<label for="lastName">Player Last Name</label>
<form:input path="lastName" /><br/>
<form:errors path="lastName"/> <br/><br/>



<label for="homePhone">Home Phone:</label>
<form:input path="homePhone" /><br/>
<form:errors path="homePhone"/> <br/><br/>

<input type="submit" name="_eventId_skip"
value="Skip"/>
<input type="submit" name="_eventId_find"
value="Find"/>
</form:form>

</body>
</html>

更新 - 我还尝试了 View 状态下的模型绑定(bind)。它还无法在我的 bean 上调用 JSR-303 注释验证。 :(

到目前为止,有效的是自定义 validate${view-state-id} 方法。见下文:

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());
}
}

流.xml

<var name="playerSearchForm" class="com.saurabhd.springwebflow.form.PlayerSearchForm"/>

<view-state id="findExistingPlayerForm" model="playerSearchForm">
<transition on="find" to="findExistingPlayerFormActionState">
</transition>
</view-state>

最佳答案

我注意到您没有在 View 状态上正确定义模型。当您定义模型时,您必须将正在使用的模型的实例设置到流范围或 View 范围中。

您正在做的是在流程中创建 PlayerSearchForm 的新实例并绑定(bind)到view-state。这不起作用,因为您必须绑定(bind)正在处理 View 的实例。

因此,您有两种方法来传递对象的引用。

第一种方式:

//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;
}

关于java - Spring Webflow - 无法在模型 bean 上调用注释验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28249878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com