Jsf 转换h:在布尔值和字符串之间选择BooleanCheckbox值
我有一个支持bean,其中包含一个字段Jsf 转换h:在布尔值和字符串之间选择BooleanCheckbox值,jsf,converter,selectbooleancheckbox,Jsf,Converter,Selectbooleancheckbox,我有一个支持bean,其中包含一个字段creditCard,该字段可以有两个字符串值y或n。我希望在复选框中显示此项,以便将y和n转换为布尔值 我如何实现它?在呈现响应时,我不能使用自定义转换器作为getAsString()返回String,而我需要布尔值组件不支持自定义转换器。属性必须是布尔值。句号 您所能做的最好的事情是在持久层中进行转换,或者添加额外的布尔getter/setter来装饰原始的y/ngetter/setter,或者完全替换旧的getter/setter。例如 private
creditCard
,该字段可以有两个字符串值y
或n
。我希望在复选框中显示此项,以便将y
和n
转换为布尔值
我如何实现它?在呈现响应时,我不能使用自定义转换器作为getAsString()
返回String
,而我需要布尔值组件不支持自定义转换器。属性必须是布尔值
。句号
您所能做的最好的事情是在持久层中进行转换,或者添加额外的布尔getter/setter来装饰原始的y
/n
getter/setter,或者完全替换旧的getter/setter。例如
private String useCreditcard; // I'd rather use a char, but ala.
public boolean isUseCreditcard() {
return "y".equals(useCreditcard);
}
public void setUseCreditcard(boolean useCreditcard) {
this.useCreditcard = useCreditcard ? "y" : "n";
}
然后在
中使用它
我也有类似的问题,我同意上一篇文章的观点,你应该在持久层处理这个问题。
然而,还有其他解决办法。我的问题是下一个:我在数据库中有一个TINYINT列,它表示布尔值true或false(0=false,1=true)。因此,我想在JSF应用程序中显示它们并将其作为布尔值处理。不幸的是,这不太可能,或者只是我没有找到一个合适的方法。但我的解决方案不是使用复选框,而是使用selectOneMeny并将这些值转换为“是”或“否”。这是代码,所以有类似问题的人可以使用它
转换器:
@FacesConverter("booleanConverter")
public class BooleanConverter implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
short number= 0;
try {
if (value.equals("Yes")) {
number= 1;
}
} catch (Exception ex) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_FATAL);
message.setSummary(MessageSelector.getMessage("error"));
message.setDetail(MessageSelector.getMessage("conversion_failed") + ex.getMessage());
throw new ConverterException(message);
}
return number;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value.toString();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
JSF页面:
<h:selectOneMenu id="selectOnePlayerSucc" value="#{vezbaTrening.izvedenaUspesno}" converter="booleanConverter">
<f:selectItems id="itemsPlayerSucc" value="#{trainingOverview.bool}" var="opt" itemValue="#{opt}" itemLabel="#{opt}"></f:selectItems>
在我的ManagedBean中,我创建了一个包含可能值的列表(“是”和“否”)
私有列表bool;
公共列表getBool(){
返回布尔;
}
公共void setBool(列表bool){
this.bool=bool;
@施工后
公共void init(){
...
bool=新链接列表();
bool.添加(“是”);
bool.添加(“否”);
}
您可以使用BooleanConverter for java原语,它将文本解析为managedbean中的布尔值,只需在.xhtml文件中输入这样的代码即可
<p:selectOneMenu id="id"
value="#{yourMB.booleanproperty}"
style="width:60px" converter="javax.faces.Boolean">
<p:ajax listener="#{yourMB.anylistener}"
update="anyIDcontrol" />
<f:selectItem itemLabel="------" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItem itemLabel="y" itemValue="true" />
<f:selectItem itemLabel="n" itemValue="false" />
</p:selectOneMenu>
为什么不能更改backingbean实例变量(creditCard)作为一个布尔值-最好不要用字符串来表示Java有对象要表示的类型。要求类型为“y”或“n”,就像在遗留数据库中是“y”或“n”一样。是的,但Java是数据库上的一个面向对象的层。如果将其作为字符串,则不能有效地使用对象。因此,在Java对象中,它是ca应该是一个布尔值,然后在SQL交互中将其转换为“y”或“n”。因此,当读取convert char to Boolean和写入convert Boolean to char.planetjones时,这正是我向客户机建议的,但客户机希望该bean只包含“y”或“n”哦,好的@Tarun-听起来你在这方面很不走运。谢天谢地,我没有ver有一个指定对象应该是什么数据类型的要求;)在这种情况下,@BalusC更改访问器以转换为布尔值听起来很合理。在执行此技巧时,请不要忘记将get
替换为is
:)为什么
不支持自定义转换器如果模型将布尔表示为非布尔(字符、数字、字符串等),则然后我会说这个模型是错误的。考虑创建一个复合组件来保持它的干燥。“这个模型将布尔值表示为非布尔值”——如果自定义模型封装一个布尔值,转换器将被包装/解开,它可能是有用的,它的问题是关于复选框,而不是DROPPDRON菜单。
<p:selectOneMenu id="id"
value="#{yourMB.booleanproperty}"
style="width:60px" converter="javax.faces.Boolean">
<p:ajax listener="#{yourMB.anylistener}"
update="anyIDcontrol" />
<f:selectItem itemLabel="------" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItem itemLabel="y" itemValue="true" />
<f:selectItem itemLabel="n" itemValue="false" />
</p:selectOneMenu>
@ManagedBean(name="yourMB")
@ViewScoped
public class YourMB implements Serializable {
private boolean booleanproperty;
public boolean isBooleanproperty() {
return booleanproperty;
}
public void setBooleanproperty(boolean booleanproperty) {
this.booleanproperty = booleanproperty;
}
}