加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JSF 2 – Bean验证:验证失败 – >空值替换为来自托

发布时间:2020-12-14 05:14:07 所属栏目:Java 来源:网络整理
导读:我不明白在价格期间JSF2的行为.希望有人可以帮助我 我有一个表单,其中字段在(ajax)提交后验证 – 好的 如果验证失败,显示错误消息 – 好的 对于我的例子,当我输入有效的生日,字段名称为空时,提交后会显示名称的错误信息. 现在当我输入一个有效的名字并从生日
我不明白在价格期间JSF2的行为.希望有人可以帮助我

我有一个表单,其中字段在(ajax)提交后验证 – 好的
如果验证失败,显示错误消息 – 好的

对于我的例子,当我输入有效的生日,字段名称为空时,提交后会显示名称的错误信息.
现在当我输入一个有效的名字并从生日字段中删除输入时,会显示一个错误消息(对于生日)(没关系),但是现在,老的“有效”生日也在输入框中显示!

如何避免这种行为?
当我提交一个空字段时,我想看到一个错误消息和一个空字段…

这是我的示例代码:

我使用包含EntityBean(Contact)的ManagedBean(TestBean).联系人每??个消息包含验证.

public class Contact implements Serializable {
    @NotNull 
    @Temporal(TemporalType.DATE)
    private Date birthday;

    @NotNull 
    @Size(min=3,max=15)
    private String name;

    //...
}

我的ManagedBean:

@ManagedBean
@ViewScoped
public class TestBean implements Serializable {
    private Contact contact;

    @PostConstruct
    void init() {
        System.out.println("init...");
        contact = new Contact(); 
    }

    public void newContact(ActionEvent ae) {
        System.out.println("newContact...");
        contact = new Contact();
    }

    public void save() {
        System.out.println("save...");
        //TODO do something with contact...
    }

    public Contact getContact() { return contact; }

    public void setContact(Contact contact) {this.contact = contact;}
}

在这里我的JSF页面:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" >      
 <h:body>
    <h:form>     
        <h:panelGrid columns="3">   

        <h:outputText value="Birthday: " />  
        <h:inputText id="birthday" value="#{testBean.contact.birthday}">
            <f:convertDateTime/>
        </h:inputText>  
        <h:message for="birthday" />  

        <h:outputText value="Name: " />  
        <h:inputText id="name" value="#{testBean.contact.name}"/>
        <h:message for="name" />

        </h:panelGrid>  

        <h:commandButton value="submit" action="#{testBean.save}"> 
            <f:ajax execute="@form" render="@form"/>
        </h:commandButton> 

        <h:commandButton value="newContact" actionListener="#{testBean.newContact}"
                         immediate="true"> 
            <f:ajax render="@form"/>
        </h:commandButton> 

    </h:form>
</h:body>
</html>

最后一个web.xml的片段

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

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

感谢一些提示

解决方法

你的具体问题是由…造成的
<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

和Mojarra的HtmlBasicRenderer#getCurrentValue()中的一个错误(至少是一个监督):

if (component instanceof UIInput) {
    Object submittedValue = ((UIInput) component).getSubmittedValue();
    if (submittedValue != null) {
        // value may not be a String...
        return submittedValue.toString();
    }
}

String currentValue = null;
Object currentObj = getValue(component);
if (currentObj != null) {
    currentValue = getFormattedValue(context,component,currentObj);
}
return currentValue;

通常,当UIInput组件成功转换和验证时,提交的值将设置为null.当JSF即将重新显示该值时,首先在继续重新显示模型值之前检查提交的值是否不为空.但是,使用此上下文参数时,它无效,而不是空字符串,因此当您删除必需字段的初始值时,它将始终重新显示原始模型值.

要测试它,将上下文参数值设置为false或完全删除.你会看到它的工作原理.然而,它将带来缺点,即您的模型值将在空的但不需要的字段上被空字符串所混淆,您将失去使用@NotNull注释JSR 303 Bean验证的优势.

要解决这个问题,你必须修改HtmlBasicRenderer#getCurrentValue()的第一部分,如下所示:

if (component instanceof UIInput && !((UIInput) component).isValid()) {
    Object submittedValue = ((UIInput) component).getSubmittedValue();
    if (submittedValue != null) {
        // value may not be a String...
        return submittedValue.toString();
    } else {
        return null;
    }
}

我已经向莫哈拉人报了issue 2262.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读