JSF 2.0 - 当空白时,selectOneMenu默认为0 [英] JSF 2.0 - selectOneMenu defaults to 0 when empty
问题描述
对于我的项目,我想使用 h:selectOneMenu 实现组合框。我读了很多关于如何正确处理selectOneMenu的帖子以及如何实现自定义转换器。组合框应存储ID( Long ),该ID位于限制条件的不同DB表(名为fk_ID)中。到目前为止,一切似乎按计划运作。现在我的问题:
For my project i want to implement a combobox with h:selectOneMenu. I read many posts about how to handle the selectOneMenu properly and how to implement a custom converter. The combobox shall store the ID (Long) from a different DB Table (named fk_ID) for which a constrain is in place. So far everything seems to function as planned. Now to my problem:
列fk_ID是可选的,因此组合框中的第一个选项是 f:selectItem ,没有标记为选择...。当我保存表单并更改值时,一切都可以,除非我将其设置为选择...。我得到的错误是约束完整性违规。
The column fk_ID is optional, so the first option in the combobox is a f:selectItem with no value labeled "choose ...". When I save the form and change the value everything is OK, except if I set it back to "choose ...". The error I get is a constraint integrity violation.
我也发现了问题:转换器返回 null 为意图,但豆接收长0 。
I found the problem too: The converter returns null as intended but the bean receives the Long 0.
XHTML:
<h:selectOneMenu value="#{userBean.selectedModel.fk_id}"
id="combobox">
<f:selectItem itemLabel="choose ..." />
<f:selectItems
value="#{userBean.items}"
var="item"
itemLabel="#{item.value}"
itemValue="#{item.id}"
/>
<f:converter converterId="userConverter" />
</h:selectOneMenu>
自定义转换器:
@FacesConverter(value = "userConverter")
public class UserConverter implements Converter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
if (arg2 != null && !arg2.equals("null"))
return Long.valueOf(arg2);
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
return String.valueOf(arg2);
}
}
该bean(非常简短的版本):
The bean (very short version):
@Named(value = "userBean")
public class UserController implements Serializable {
@Getter
@Setter
private UserModel selectedModel;
@Getter
@Setter
private List<UserModel> items;
public void saveModel() {
selectedModel = userService.saveModel(selectedModel);
}
}
问题是, fk_id 是 Long 0 ,但应该是 null 。如果(selectedModel.getFk_id()== 0)
selectedModel.setFk_id(),我可以通过编写
The problem is that the fk_id is Long 0, but should be null. I can solve the problem by writing
if (selectedModel.getFk_id() == 0)
selectedModel.setFk_id(null);
,但这不是我很满意的解决方案。
, but thats not a solution I am comfortable with.
我的问题:为什么转换器返回的值 null , saveModel()方法中的 Long 0
My Question: Why is the value null, which the converter returns, the Long 0 inside the saveModel() method?
推荐答案
此转换器是不必要的,没有任何意义(我希望 UserConverter
在用户
和之间转换
,而不在 Long
和 String
)。去掉它。
This converter is unnecessary and makes no sense (I'd expect an UserConverter
to convert between User
and String
, not between Long
and String
). Remove it.
现在,您需要告诉JSF将空字符串提交的值解释为 null
。将以下内容添加到您的 web.xml
中:
Now, you need to tell JSF to interpret empty string submitted values as null
. Add the following to your web.xml
:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
这应该可以解决大多数容器的问题。但是如果您使用Tomcat或其叉/克隆(JBoss AS,WebSphere AS等)和/或容器使用Apache EL解析器实现,那么还需要将以下参数添加到其VM启动参数中:
This should solve the problem on most containers. But if you're using Tomcat or a fork/clone of it (JBoss AS, WebSphere AS, etc) and/or the container uses the Apache EL parser implementation, then you also need to add the following argument to its VM startup arguments:
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
这将阻止Apache EL解析器将提交的值强制为原语的默认值,即使您使用原始包装器类型作为属性。
This will prevent the Apache EL parser from coercing the submitted value to the primitive's default, even though you're using a primitive wrapper type as property.
- Communication in JSF 2.0
不相关的具体问题,你没有一个组合框。这只是一个简单的下拉列表。组合框是可编辑的下拉列表。获取您的条款正确;)
Unrelated to the concrete problem, you don't have a combobox there. That's just a simple dropdownlist. A combobox is an editable dropdownlist. Get your terms right ;)
这篇关于JSF 2.0 - 当空白时,selectOneMenu默认为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!