当javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为TRUE时,必填字段问题 [英] Required field issue when javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL set to TRUE

查看:75
本文介绍了当javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为TRUE时,必填字段问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MyFaces和Mojarra 2.1中,都存在一个缺陷,当javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为true时,模型中标记为必填并在模型中预先填充的所有字段(当空白和提交时都将被重新填充)以其原始非空白值显示,而不是留空.

In both MyFaces and Mojarra 2.1, there exists a defect where when javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to true, any fields that are marked as required and pre-populated in the model, when blanked-out and submitted are re-shown with their original non-blanked-out values instead of being left blank.

场景是:

  1. 用户使用单个必填字段加载页面,其中填充了模型中的现有数据
  2. 用户清除页面上的字段并提交表单
  3. 验证失败,正如预期的那样,并且向用户显示一条错误消息,要求他们必须填写 必填字段.
  1. User loads page with a single required field that is populated with existing data from the model
  2. User clears out the field on the page and submits the form
  3. Validation fails, as expected, and the user is shown an error message that they must fill in the required field.

问题在于,应使用模型中的原始值填充该字段,该字段应显示用户提交的内容(即,该用户为该字段提交了空白值).唯一的解决方法是将javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为false.设置为false可提供所需的行为,即在重新显示带有所需字段错误消息的页面时,该字段保持空白.

The issue is that the field, which should show what the user submitted -- which is that they submitted a blank value for the field -- is instead populated with the original value from the model. The only workaround is to set javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to false. Setting to false delivers the desired behavior that the field remains blanked out when the page is redisplayed with the required field error message.

Mojarra(http://java.net/jira/browse/JAVASERVERFACES-2375)和MyFaces(https://issues.apache.org/jira/browse/MYFACES-3525)记录了一个缺陷,但是没有任何进展已经制作了6周.

A defect was logged with Mojarra (http://java.net/jira/browse/JAVASERVERFACES-2375) and MyFaces (https://issues.apache.org/jira/browse/MYFACES-3525) but no progress has been made in 6 weeks.

请注意,Mojarra在6个月内似乎也报告了类似的问题 以前尚未取得任何进展.

Note that it appears Mojarra had a similar issue reported over 6 mos ago yet no progress has been made.

有人知道这种解决方法吗,我们可以将javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为true而不是false,而不会遇到此必填字段可用性问题?

Does anyone know of a workaround for this where we can keep javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL set to true instead of false and yet not encounter this required field usability issue?

推荐答案

此问题在

This problem is explained in detail in JSF 2 - Bean Validation: validation failed -> empty values are replaced with last valid values from managed bean. To the point, in Mojarra it's caused by a bug or at least an oversight in HtmlBasicRenderer#getCurrentValue() and has been reported as issue 2262.

同时,解决此问题的最简单方法是,在考虑到第三方组件库及其自身的渲染器(例如PrimeFaces)的同时,将UIInput的源文件直接复制到项目的源文件夹中,然后编辑getSubmittedValue()相应地:

In the meanwhile, the easiest way to workaround this, while taking 3rd party component libraries with their own renderers like PrimeFaces into account, is to copy the source file of UIInput straight in your project's source folder and then edit the getSubmittedValue() accordingly:

public Object getSubmittedValue() {
    if (submittedValue == null && !isValid() && considerEmptyStringNull(FacesContext.getCurrentInstance())) {
        return "";
    }
    else {
        return submittedValue;
    }
}

它将最终出现在/WEB-INF/classes中,这将在JSF JAR文件中的类加载之上获得优先级.诚然,它有些笨拙,但比重建JSF JAR文件或覆盖每个输入渲染器要容易得多.

It'll end up in /WEB-INF/classes and this will get precedence in classloading above the one in the JSF JAR file. Admittedly, it's somewhat clumsy, but it's less painful than rebuilding the JSF JAR file or overriding every single input renderer.

这篇关于当javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为TRUE时,必填字段问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆