原来想用a4j和richfaces做一点rich应用,没想到动态填充SelectItems的SelectOneMenu不能绑定值到bean。错误提示"value is not valid"。有人说是转换的时候出了问题要重写equal函数,但是我只使用字符串作为SelectItem的 label and key. 难道字符串之间也不能比较吗?不能再浪费时间了,以后有时间找到JSF的源码看看。现在还是老老实实多做几个页面算了。
页面:
RegionBean:
DealerList:
页面:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">
<ui:define name="body">
<div class="title"><h:outputText
value="View dealers in a region" /></div>
<h:form id="forma">
<h:outputText value="Please select a country:" />
<h:selectOneRadio value="#{Regions.country}">
<f:selectItem itemLabel="USA" itemValue="USA" />
<f:selectItem itemLabel="CANADA" itemValue="CANADA" />
<a:support event="onchange" reRender="regionPanel" />
</h:selectOneRadio>
</h:form>
<h:form id="formb">
<a:outputPanel id="regionPanel">
<s:decorate id="regionDecoration" template="layout/display.xhtml">
<ui:define name="label">Select a region:</ui:define>
<!-- <h:selectOneMenu id="selectRegion" value="#{dealerList.region}" converter="SelectItemConverter">-->
<rich:comboBox id="selectRegion" value="#{dealerList.region}"
converter="SelectItemConverter">
<f:selectItems value="#{Regions.regionItems}" />
</rich:comboBox>
<!-- </h:selectOneMenu>-->
</s:decorate>
</a:outputPanel>
<div class="actionButtons"><a:commandButton
id="showDealersButton" reRender="dealerResults" value="Show Dealers">
</a:commandButton> <a:commandButton id="showAllDealersButton" reRender="dealerResults"
value="Show All Dealers" action="#{dealerList.setRegion('%')}">
</a:commandButton></div>
</h:form>
<h:messages globalOnly="true" styleClass="message" />
<a:outputPanel id="dealerResults">
<h:outputText value="OutText:Current region is: #{dealerList.ejbql}" />
<h:inputText
value="InputText:Current region is: #{dealerList.region}" />
<rich:panel>
<f:facet name="header">dealerList</f:facet>
<div class="results"><h:outputText value="No dealer exists"
rendered="#{empty dealerList.resultList}" /> <h:dataTable
id="dealerList" var="dealer" value="#{dealerList.resultList}"
rendered="#{not empty dealerList.resultList}">
<h:column>
<f:facet name="header">Id</f:facet>
<h:outputText id="dealderId" value="#{dealer.id}">
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<s:link id="dealer" value="#{dealer.name}" view="/dealer.xhtml">
<f:param name="dealerId" value="#{dealer.id}" />
</s:link>
</h:column>
<h:column>
<f:facet name="header">Region</f:facet>
<h:outputText id="region" value="#{dealer.region}"></h:outputText>
</h:column>
</h:dataTable></div>
</rich:panel>
</a:outputPanel>
<div class="actionButtons"><s:button id="done"
value="Create dealer" view="/dealer.xhtml" /></div>
</ui:define>
</ui:composition>
RegionBean:
package com.govr.catalog.session;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.faces.model.SelectItem;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;
import org.jboss.seam.faces.FacesMessages;
import com.govr.catalog.entity.CanadaProvinces;
import com.govr.catalog.entity.USAStates;
//@Stateless
@Stateful
@Name("Regions")
public class RegionsBean implements Regions {
@Logger
private Log log;
@In
FacesMessages facesMessages;
public void regions() {
// implement your business logic here
log.info("Regions.regions() action called");
facesMessages.add("regions");
}
private String country;
private String selectARegionLabel;
public List getRegionList(String country) {
List l = new ArrayList();
if ("USA".equals(country))
l = USAStates.getRegionList();
if ("CANADA".equals(country))
l = CanadaProvinces.getRegionList();
return l;
}
public Map getRegionMap(String country) {
Map l = new HashMap();
if ("USA".equals(country))
for (String s : USAStates.states)
l.put(s, s);
if ("CANADA".equals(country))
for (String s : CanadaProvinces.states)
l.put(s, s);
return l;
}
public List getRegionList() {
List l = new ArrayList();
if ("USA".equals(country))
l = USAStates.getRegionList();
if ("CANADA".equals(country))
l = CanadaProvinces.getRegionList();
return l;
}
public List<SelectItem> getRegionItems() {
List<SelectItem> l = new ArrayList<SelectItem>();
// l.add(new SelectItem(""));
if ("USA".equals(country)) {
for (String s : USAStates.states)
l.add(new SelectItem(s,s));
setSelectARegionLabel("Please select a state:");
} else if ("CANADA".equals(country)) {
for (String s : CanadaProvinces.states)
l.add(new SelectItem(s,s));
setSelectARegionLabel("Please select a provice:");
} else {
l.add(new SelectItem("No region to show","No region to show"));
}
return l;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
public void setSelectARegionLabel(String selectARegionLabel) {
this.selectARegionLabel = selectARegionLabel;
}
public String getSelectARegionLabel() {
return selectARegionLabel;
}
@Remove
@Destroy
public void destroy() {
}
// add additional action methods
}
DealerList:
package com.govr.catalog.session;
import javax.faces.model.SelectItem;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.framework.EntityQuery;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.ScopeType;
@Name("dealerList")
@Scope(ScopeType.CONVERSATION)
public class DealerList extends EntityQuery {
// @RequestParameter
// String region = "";
// @In
private String region;
private SelectItem selectedRegionItem;
@Override
public String getEjbql() {
setRegion(((String)getRegion()).trim());
return "select dealer from Dealer dealer where dealer.region like '"
+ getRegion() + "'";
}
public void setRegion(String region) {
this.region = region;
}
public String getRegion() {
if (region == null)
setRegion("");
return region;
}
public void setSelectedRegionItem(SelectItem selectedRegionItem) {
this.selectedRegionItem = selectedRegionItem;
setRegion((String) this.selectedRegionItem.getValue());
}
public SelectItem getSelectedRegionItem() {
return selectedRegionItem;
}
}