gpt4 book ai didi

jsf-2 - 空输入值不会触发@NotNull,但会触发@NotBlank

转载 作者:行者123 更新时间:2023-12-04 06:07:03 32 4
gpt4 key购买 nike

我有这个实体:

import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;

@Entity
@XmlRootElement
@Table
public class Supplier extends AbstractEntity
{
@Column
@NotNull
private String name;

@Column
@NotBlank
private String representative;

...
}

和这个 jsf 形式:
<h:form>
<p:commandButton value="save" action="#{supplierController.create}" ajax="false"/>

<h:panelGrid columns="3" cellpadding="4">
<h:outputLabel value="#{bundle['Name']}" for="name"/>
<p:inputText id="name" value="#{supplierController.value.name}"/>
<p:message for="name"/>

<h:outputLabel value="#{bundle['Representative']}" for="representative"/>
<p:inputText id="representative" value="#{supplierController.value.representative}"/>
<p:message for="representative"/>
</h:panelGrid>
</h:form>

那么,为什么是 @NotBlank触发和 @NotNull不是?

我在 glassfish-3.1.1 下使用 mojarra-2.1.3_01 (ships hibernate-validator-4.2.0.Final?)

最佳答案

由于 HTTP 的性质,空请求参数被检索为空字符串而不是 null .所以@NotNull验证器永远不会被触发。它只会在表单中缺少该字段或禁用时触发。

但是,您可以通过将以下上下文参数添加到 web.xml 来配置 JSF 2.x 以将空提交的值解释为 null。 :

<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>

这样 @NotNull将为空字段触发。

关于jsf-2 - 空输入值不会触发@NotNull,但会触发@NotBlank,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8235990/

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