使用“请选择"f:selectItem 在 p:selectOneMenu 中具有 null/空值 [英] Using a "Please select" f:selectItem with null/empty value inside a p:selectOneMenu
问题描述
我正在从数据库中填充一个
,如下所示.
I'm populating a <p:selectOneMenu/>
from database as follows.
<p:selectOneMenu id="cmbCountry"
value="#{bean.country}"
required="true"
converter="#{countryConverter}">
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
<f:selectItems var="country"
value="#{bean.countries}"
itemLabel="#{country.countryName}"
itemValue="#{country}"/>
<p:ajax update="anotherMenu" listener=/>
</p:selectOneMenu>
<p:message for="cmbCountry"/>
默认选择的选项,当这个页面被加载时,
The default selected option, when this page is loaded is,
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
转换器:
@ManagedBean
@ApplicationScoped
public final class CountryConverter implements Converter {
@EJB
private final Service service = null;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
//Returns the item label of <f:selectItem>
System.out.println("value = " + value);
if (!StringUtils.isNotBlank(value)) {
return null;
} // Makes no difference, if removed.
long parsedValue = Long.parseLong(value);
if (parsedValue <= 0) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Message"));
}
Country entity = service.findCountryById(parsedValue);
if (entity == null) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Message"));
}
return entity;
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Message"), e);
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value instanceof Country ? ((Country) value).getCountryId().toString() : null;
}
}
当来自
< f:selectItem>
表示的菜单中的第一个项目,然后提交表单,在 getasobject中获取的
方法是 值
()Select
,它是
的标签 - 列表中的第一项,直观上根本没有预期.
When the first item from the menu represented by <f:selectItem>
is selected and the form is submitted then, the value
obtained in the getAsObject()
method is Select
which is the label of <f:selectItem>
- the first item in the list which is intuitively not expected at all.
当
的 itemValue
属性设置为空字符串时,它会抛出 java.lang.NumberFormatException: For input string: ""
在 getAsObject()
方法中,即使异常被精确捕获并注册为 ConverterException
.
When the itemValue
attribute of <f:selectItem>
is set to an empty string then, it throws java.lang.NumberFormatException: For input string: ""
in the getAsObject()
method even though the exception is precisely caught and registered for ConverterException
.
当 getAsString()
的 return
语句从
return value instanceof Country?((Country)value).getCountryId().toString():null;
到
return value instanceof Country?((Country)value).getCountryId().toString():"";
null
被一个空字符串替换,但当所讨论的对象为 null
时返回一个空字符串,进而导致另一个问题,如此处.
null
is replaced by an empty string but returning an empty string when the object in question is null
, in turn incurs another problem as demonstrated here.
如何使此类转换器正常工作?
How to make such converters work properly?
也尝试过 org.omnifaces.converter.SelectItemsConverter
但没有任何区别.
Also tried with org.omnifaces.converter.SelectItemsConverter
but it made no difference.
推荐答案
When the select item value is null
, then JSF won't render <option value>
,但只有 .因此,浏览器将改为提交选项的标签.这在 HTML 规范(强调我的):
When the select item value is null
, then JSF won't render <option value>
, but only <option>
. As consequence, browsers will submit the option's label instead. This is clearly specified in HTML specification (emphasis mine):
value = cdata [CS]
该属性指定控件的初始值.如果未设置该属性,则将初始值设置为 OPTION 元素的内容.
This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.
您也可以通过查看 HTTP 流量监视器来确认这一点.您应该会看到正在提交的选项标签.
You can also confirm this by looking at HTTP traffic monitor. You should see the option label being submitted.
您需要将选择项值设置为空字符串.然后 JSF 将呈现
You need to set the select item value to an empty string instead. JSF will then render a <option value="">
. If you're using a converter, then you should actually be returning an empty string ""
from the converter when the value is null
. This is also clearly specified in Converter#getAsString()
javadoc (emphasis mine):
getAsString
...
返回:如果值为空,则返回一个零长度的字符串,否则为转换结果
因此,如果您将 <f:selectItem itemValue="#{null}>>
与这样的转换器结合使用,则 <option value="">
将被渲染,浏览器将只提交一个空字符串而不是选项标签.
So if you use <f:selectItem itemValue="#{null}">
in combination with such a converter, then a <option value="">
will be rendered and the browser will submit just an empty string instead of the option label.
至于处理提交的空字符串值(或 null
),您实际上应该让您的转换器将此责任委托给 required="true"
属性.因此,当传入的 value
为 null
或空字符串时,您应该立即返回 null
.基本上你的实体转换器应该如下实现:
As to dealing with the empty string submitted value (or null
), you should actually let your converter delegate this responsibility to the required="true"
attribute. So, when the incoming value
is null
or an empty string, then you should return null
immediately. Basically your entity converter should be implemented like follows:
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return ""; // Required by spec.
}
if (!(value instanceof SomeEntity)) {
throw new ConverterException("Value is not a valid instance of SomeEntity.");
}
Long id = ((SomeEntity) value).getId();
return (id != null) ? id.toString() : "";
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null; // Let required="true" do its job on this.
}
if (!Utils.isNumber(value)) {
throw new ConverterException("Value is not a valid ID of SomeEntity.");
}
Long id = Long.valueOf(value);
return someService.find(id);
}
至于您对此的特殊问题,
As to your particular problem with this,
但是当有问题的对象为空时返回一个空字符串,反过来又会引发另一个问题,如此处.
正如那边的回答,这是 Mojarra 中的一个错误,并且从 OmniFaces 1.8 开始在
中被绕过.因此,如果您至少升级到 OmniFaces 1.8.3 并使用其
而不是
,那么您不应该受到影响不再被这个错误.
As answered over there, this is a bug in Mojarra and bypassed in <o:viewParam>
since OmniFaces 1.8. So if you upgrade to at least OmniFaces 1.8.3 and use its <o:viewParam>
instead of <f:viewParam>
, then you shouldn't be affected anymore by this bug.
OmniFaces SelectItemsConverter
在这种情况下也应该能正常工作.它为 null
返回一个空字符串.
The OmniFaces SelectItemsConverter
should also work as good in this circumstance. It returns an empty string for null
.
这篇关于使用“请选择"f:selectItem 在 p:selectOneMenu 中具有 null/空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!