selectOneMenu with a4j:support to rerender another selectOne
mean Dec 25, 2008 7:27 PMHello,
I have two selectOneMenus. If a user selects the marke/brand of a car (e.g. BMW, VW, Mercedes), the second selectOneMenu should show the different types of cars depending on the selected marke/brand (e.g. E-Klasse, S-Klasse,...).
I spend a whole day (and half night) on searching for results for my problem in google,... But found nothing.
It works so far, but if I choose the type of the car, I get the following error:
01:11:07,405 INFO [lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=newUsedCar:fahrzeugTyp[severity=(ERROR 2), summary=(newUsedCar:fahrzeugTyp: Validation-error: Value is not valid.), detail=(newUsedCar:fahrzeugTyp: Validation-error: Value is not valid.)]
This is my code in the xhtml-page
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:a4j="http://richfaces.org/a4j"
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:rich="http://richfaces.org/rich"
xmlns:c="http://java.sun.com/jstl/core"
template="../layout/template.xhtml">
<ui:define name="body">
<h:messages />
<h:form id="newUsedCar">
<s:validateAll>
<h:outputText value="Land:" />
<a4j:region id="filter">
<h:panelGroup id="countryOneMenu">
<h:selectOneMenu value="#{newUsedCarAction.marke}" id="marke">
<s:selectItems var="marke" value="#{newUsedCarAction.markenSelect()}" label="#{marke.marke}" itemValue="#{marke.marke}" noSelectionLabel="Marke" />
<a4j:support event="onchange" action="#{newUsedCarAction.changeTyp()}" reRender="statesOneMenu" ajaxSingle="true"/>
</h:selectOneMenu>
</h:panelGroup>
<h:outputText value="Bundesstaat oder was auch immer:" />
<h:panelGroup id="statesOneMenu">
<h:selectOneMenu value="#{newUsedCarAction.typ}" id="fahrzeugTyp">
<s:selectItems var="fahrzeugTyp" value="#{newUsedCarAction.getFahrzeugTypen()}" label="#{fahrzeugTyp.fahrzeugTyp}" itemValue="#{fahrzeugTyp.fahrzeugTyp}" noSelectionLabel="FahrzeugTyp" />
<a4j:support event="onchange"/>
</h:selectOneMenu>
</h:panelGroup>
</a4j:region>
<h:commandButton action="#{newUsedCarAction.sellCar()}" value="submitButton" />
</s:validateAll>
</h:form>
</ui:define>
</ui:composition>
This is my entityBean
package de.autohaus;
import static org.jboss.seam.ScopeType.CONVERSATION;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import static javax.persistence.PersistenceContextType.EXTENDED;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.log.Log;
import de.autohaus.fahrzeugverwaltung.Fahrzeugverwaltung;
import de.autohaus.fahrzeugverwaltung.db.Fahrzeug;
import de.autohaus.fahrzeugverwaltung.db.FahrzeugTypNotFoundException;
import de.autohaus.fahrzeugverwaltung.db.Marke;
import de.autohaus.fahrzeugverwaltung.db.FahrzeugTyp;
import de.autohaus.bestellverwaltung.Bestellverwaltung;
import de.autohaus.benutzerverwaltung.db.Anrede;
import de.autohaus.benutzerverwaltung.db.Kunde;
import de.autohaus.bestellverwaltung.db.Versandart;
@Stateful
@Scope(CONVERSATION)
@Name("newUsedCarAction")
public class NewUsedCarAction implements NewUsedCar {
@Logger
Log log;
@EJB
private Fahrzeugverwaltung fv;
@EJB
private Bestellverwaltung bv;
@PersistenceContext
private EntityManager em;
@In
private transient FacesContext facesContext;
@In
private Kunde kunde;
private ArrayList<Versandart> versandarten;
private ArrayList<Marke> marken;
private ArrayList<FahrzeugTyp> typen;
private String versandart;
private Fahrzeug fahrzeug;
private String marke;
private String typ;
private String verify;
public void sellCar() {
log.info("TYP: " + typ);
log.info("Marke: " + marke);
}
public List<Marke> markenSelect() {
marken = new ArrayList<Marke>(fv.findAllMarken());
return marken;
}
public List<FahrzeugTyp> getFahrzeugTypen() {
if(this.typen == null)
this.typen = new ArrayList<FahrzeugTyp>();
log.info("FahrzeugTypen: " + typen);
return this.typen;
}
public List<FahrzeugTyp> changeTyp() throws FahrzeugTypNotFoundException {
typen = new ArrayList<FahrzeugTyp>(fv.findFahrzeugTypenByMarke(this.marke));
return typen;
}
public List<Versandart> versandartenSelect() {
log.info("BV: " + bv);
versandarten = new ArrayList<Versandart>(bv.findAllVersandarten());
log.info(versandarten);
return versandarten;
}
public String getTyp() {
return this.typ;
}
public void setTyp(String t) {
this.typ = t;
}
public String getMarke() {
return this.marke;
}
public void setMarke(String m) {
this.marke = m;
}
public String getVersandart() {
return this.versandart;
}
public void setVersandart(String v) {
this.versandart = v;
}
public Kunde getKunde() {
return this.kunde;
}
public void setKunde(Kunde k) {
this.kunde = k;
}
public Fahrzeug getFahrzeug() {
return this.fahrzeug;
}
public void setFahrzeug(Fahrzeug f) {
this.fahrzeug = f;
}
public String getVerify()
{
return verify;
}
public void setVerify(String verify)
{
this.verify = verify;
}
@Remove
public void destroy() {}
}
Something very strange is: This example works on an other page. This page is started by clicking a button <h:commandLink value="show" action="#{buyCarAction.buyCar(car)}" rendered="#{identity.loggedIn}" />. As paramter I send a Fahrzeug-Object.
The code in the page and also of the entity are completely the same (I only changed the @Name of the bean and according in the xhtml-page). And this works perfectly...
Thanks a lot and best regards,
Dirk