[英]How can I create a JSF composite component that binds a Collection to both an h:selectOneMenu and h:selectManyListbox?
I'm trying to create a composite component that will allow the user to toggle between ah:singleSelectMenu and h:selectManyListbox. 我正在尝试创建一个复合组件,该组件将允许用户在ah:singleSelectMenu和h:selectManyListbox之间切换。 I have it working sort of. 我有它的工作。 It works as long as the value field points to a Collection...it does NOT work if the value field is null. 只要value字段指向Collection,它就起作用...如果value字段为null,则不起作用。
singleMultiSelect.xhtml singleMultiSelect.xhtml
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE html>
<ui:composition
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
>
<cc:interface componentType="singleMultiSelect">
<!-- The initial list of objects -->
<cc:attribute name="list" type="java.util.List" required="true"/>
<!-- The selected objects -->
<cc:attribute name="selected" type="java.util.Collection" required="true"/>
<!-- whether to display the selectOneMenu (true) or selectManyBox (false) -->
<cc:attribute name="singleSelect" type="java.lang.Boolean"
required="false" default="true"/>
</cc:interface>
<cc:implementation>
<span id="#{cc.clientId}">
<ace:checkboxButton id="singleSelectChkBx"
value="#{cc.attrs.singleSelect}">
<ace:ajax render="#{cc.clientId}"/>
</ace:checkboxButton>
<h:selectOneMenu id="selectOneMenu"
rendered="#{cc.attrs.singleSelect}"
value="#{cc.singleSelected}">
<f:selectItems value="#{cc.attrs.list}"/>
</h:selectOneMenu>
<h:selectManyListbox id="selectManyListbox"
rendered="#{! cc.attrs.singleSelect}"
value="#{cc.attrs.selected}">
<f:selectItems value="#{cc.attrs.list}"/>
</h:selectManyListbox>
</span>
</cc:implementation>
</ui:composition>
SingleMultiSelect.java SingleMultiSelect.java
public class SingleMultiSelect extends UINamingContainer {
public SingleMultiSelect() {
super();
}
/**
* Converts the Object selected within the selectOneMenu to the list
* used by the component.
*
* @param singleSelected
*/
public void setSingleSelected(Object singleSelected) {
getSelected().clear();
if(singleSelected != null) {
getSelected().add(singleSelected);
}
}
/**
* Converts the collection used by the component to a single
* Object selected within the selectOneMenu.
*
* @return
*/
public Object getSingleSelected() {
return getSelected().size() > 0 ? getSelected().iterator().next() : null;
}
private Collection getSelected() {
return (Collection) getAttributes().get("selected");
}
}
I tried writing to the attributes map but that didn't work 我尝试写属性映射,但是没有用
public void setSingleSelected(Object singleSelected) {
HashSet selected = new HashSet();
selected.add(singleSelected);
((Collection) getAttributes()).put("selected", selected);
}
getAttributes().put("selected", selected);
You're basically overriding the underlying ValueExpression
object #{cc.attrs.selected}
with a HashSet
. 您基本上是使用HashSet
覆盖基础ValueExpression
对象#{cc.attrs.selected}
。 In other words, the EL expression became a "hardcoded" value and can't reach the bean property anymore. 换句话说,EL表达式成为一个“硬编码”值,并且不再具有bean属性。
You should be obtaining the ValueExpression
and invoke the setter via setValue()
call. 您应该获取ValueExpression
并通过setValue()
调用来调用setter。
getValueExpression("selected").setValue(context.getElContext(), selected);
Unrelated to the concrete problem, when implementing a backing component, it's better to think like an UI component, not a backing bean. 与具体问题无关 ,在实现支持组件时,最好像UI组件而不是支持bean那样思考。 Don't manipulate the model directly (the composite attributes), but the composite component's own components which you reach via binding
. 不要直接操纵模型(复合属性),而要通过binding
访问复合组件自己的组件。 Moreover, do not touch the getters/setters. 此外,请勿触摸吸气剂/吸气剂。 Replace the <h:selectOneMenu value>
as below: 替换<h:selectOneMenu value>
,如下所示:
<h:selectOneMenu binding="#{cc.singleSelected}" ...>
private UISelectOne singleSelected; // +getter+setter
@Override
public void processUpdates(FacesContext context) {
super.processUpdates(context);
if (getAttributes().get("singleSelect") == Boolean.TRUE) {
HashSet selected = new HashSet();
if (singleSelected.getValue() != null) {
selected.add(singleSelected.getValue());
}
getValueExpression("selected").setValue(context.getELContext(), selected);
}
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
if (getAttributes().get("singleSelect") == Boolean.TRUE) {
Collection selected = (Collection) getAttributes().get("selected");
if (selected != null && !selected.isEmpty()) {
singleSelected.setValue(selected.iterator().next());
} else {
singleSelected.setValue(null);
}
}
super.encodeBegin(context);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.