选择项目列表时,使用Ajax将事件触发到Web服务器。该元素是JSF rich:orderingList
项。
必须接收该事件的类:
public class BusinessAreaListHandler extends ListHandler<ListElement> {
private static final long serialVersionUID = -581048454118449233L;
public BusinessAreaListHandler() { load(); }
public List<ListElement> getBusinessAreas() { return getList(); }
public Set<ListElement> getSelection() { return getSet(); }
public void select() {
System.out.println( "Clicked Element" );
}
protected void load() {
appendList( new ListElement( "employee" ) );
appendList( new ListElement( "company" ) );
appendList( new ListElement( "payroll" ) );
}
}
ListElement
容器对象:
public class ListElement implements Serializable {
private static final long serialVersionUID = 5396525520175914504L;
public final static char SEPARATOR = ':';
private String id, value;
protected ListElement( String value ) {
this( Integer.toString( value.hashCode() ), value );
}
public ListElement( String id, String value ) {
setId( id );
setValue( value );
}
@Override
public int hashCode() {
return (53 * (getId().hashCode() + 5)) + (53 * (getValue().hashCode() + 5));
}
@Override
public boolean equals( Object obj ) {
ListElement le = null;
if( obj != null && obj instanceof ListElement ) {
le = (ListElement)obj;
}
return le == null ? false : compare( le );
}
private boolean compare( ListElement le ) {
return getId().equals( le.getId() ) && getValue().equals( le.getValue() );
}
public String getId() {
return this.id == null ? "" : this.id;
}
public void setId( String id ) {
this.id = id;
}
public String getValue() {
return this.value == null ? "" : this.value;
}
public void setValue( String value ) {
this.value = value;
}
@Override
public String toString() {
return getValue();
}
}
ListConverter
:
public class ListConverter implements Converter {
@Override
public Object getAsObject( FacesContext fc, UIComponent ui, String value ) {
ListElement result = new ListElement( value );
int index = value.lastIndexOf( ListElement.SEPARATOR );
System.out.println( "Convert FROM: " + value );
if( index > 0 ) {
String id = value.substring( 0, index );
String v = value.substring( index + 1 );
result = new ListElement( id, v );
}
System.out.println( "Convert TO : " + result.toString() );
return result;
}
@Override
public String getAsString( FacesContext fc, UIComponent ui, Object value ) {
return value.toString();
}
}
设置列表和事件操作的XHTML片段:
<h:panelGrid columns="2">
<h:panelGroup>
<rich:orderingList value="#{businessAreas.list}" var="businessArea" converter="businessAreaConverter" immediate="true" orderControlsVisible="false" fastOrderControlsVisible="false" selection="#{businessAreas.selection}" id="BusinessAreas">
<f:facet name="caption">
<h:outputText value="Business Areas" />
</f:facet>
<rich:column>
<h:outputText value="#{businessArea}" />
</rich:column>
<a4j:support event="onclick" ignoreDupResponses="true" requestDelay="500" action="#{businessAreas.select}" reRender="ColumnClusters" />
<a4j:support event="onkeyup" ignoreDupResponses="true" requestDelay="500" action="#{businessAreas.select}" reRender="ColumnClusters" />
</rich:orderingList>
</h:panelGroup>
<h:panelGroup>
<rich:listShuttle sourceValue="#{columnClusters.list}" targetValue="#{domainContent.list}" var="columnCluster" converter="columnClusterConverter" sourceRequired="false" targetRequired="true" moveControlsVisible="true" orderControlsVisible="false" fastOrderControlsVisible="false" sourceCaptionLabel="Column Clusters" targetCaptionLabel="Domain Content" copyTitle="Move" copyControlLabel="Move" copyAllControlLabel="Move All" copyAllTitle="Move All" id="ColumnClusters">
<rich:column>
<h:outputText value="#{columnCluster}" />
</rich:column>
</rich:listShuttle>
</h:panelGroup>
<h:panelGroup>
<h:commandButton action="action" value="Create Domain" />
</h:panelGroup>
</h:panelGrid>
相应的托管bean在faces-config.xml
中正确设置。
浏览器输出:
显示以下错误消息:
信息:警告:FacesMessage已入队,但可能尚未显示。 sourceId = j_id2:ColumnClusters [severity =(ERROR 2),summary =(j_id2:ColumnClusters: 验证错误:值是必需的。),detail =(j_id2:ColumnClusters:验证错误: 值是必需的。)]
New Element (Hash:Value): 1193469614:employee
New Element (Hash:Value): 950484093:company
New Element (Hash:Value): -786522843:payroll
New Element (Hash:Value): 3373707:name
New Element (Hash:Value): -1147692044:address
New Element (Hash:Value): 114603:tax
New Element (Hash:Value): 1193469614:employee
Convert FROM: employee
Convert TO : employee
New Element (Hash:Value): 950484093:company
Convert FROM: company
Convert TO : company
New Element (Hash:Value): -786522843:payroll
Convert FROM: payroll
Convert TO : payroll
New Element (Hash:Value): 3373707:name
Convert FROM: name
Convert TO : name
New Element (Hash:Value): -1147692044:address
Convert FROM: address
Convert TO : address
New Element (Hash:Value): 114603:tax
Convert FROM: tax
Convert TO : tax
当用户选择(或取消选择)项目时,在select()
中触发对BusinessAreaListHandler
的调用需要什么?
谢谢!
答案 0 :(得分:2)
问题在于这个属性:
targetRequired="true"
由于click会立即向服务器发出Ajax请求,因此预计右侧穿梭(目标)会有数据。如果没有数据,将发布验证错误。