2
votes

i searched a lot about this subject but i didn't find the solution that helps me with my problem. I have a form that consists of list of "Kategorie"-objects which are placed as selectItems in selectOneMenu and an outputText (uses same bean as selectOneMenu). The converter makes his job and all Kategorie names are shown correctly. With the ajax-tag the form changes the value in selectOneMenu onchange. This also works, the bean-property is updated! But now i want to show the selected Kategorie in an outputText immediatly after change. What do i have to do???

To see the new value in the outputText I have to refresh the page. (KategorieBean is SessionScoped)

The getter of the outputText-value is always called before the Kategorie-property of the bean has been updated.

view.xhtml:

    <h:form>
        Wähle eine Kategorie:
        <h:selectOneMenu 
                            value="#{kategorieBean.kategorie}">
            <f:selectItem
                            itemLabel="Wählen..."
                            itemValue="#{null}"/>
            <f:selectItems 
                            value="#{kategorieBean.allKategories}"
                            var="k" 
                            itemLabel="#{k.name}"
                            itemValue="#{k}" />     
            <f:ajax 
                            event="change"
                            execute="@form"
                            render="result"
                            listener="#{kategorieBean.kategorieChangeListener}"/>
        </h:selectOneMenu>
        Ausgewählte Kategorie:      
        <h:outputText id="result" value="#{kategorieBean.kategorie.name}" />
    </h:form>

I also tried to use a Listener in the ajax-tag. The Listener worked, but I still had the problem with the execution-order :(

KategorieBean:

package xxx.bean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import xxx.hibernate.entity.Kategorie;
import xxx.hibernate.dao.KategorieDao;

@ManagedBean(name="kategorieBean")
@SessionScoped
public class KategorieBean{

private Kategorie kategorie;
private KategorieDao kategorieDao;
private List<Kategorie> allKategories;

@PostConstruct
public void init(){
    kategorie = null;
    kategorieDao = new KategorieDao();
    allKategories = kategorieDao.getAllKategories();
}

public List<Kategorie> getAllKategories() {
    return allKategories;
}
public void setAllKategories(List<Kategorie> allKategories) {
    this.allKategories = allKategories;
}

public Kategorie getKategorie() {
    if (kategorie != null)
        System.out.println("getter kategorie: name=" + kategorie.getName());
    else
        System.out.println("getter kategorie: kategorie=null");
    return kategorie;
}
public void setKategorie(Kategorie kategorie) {
    System.out.println("setter kategorie: name=" + kategorie.getName());
    this.kategorie = kategorie;
}
    public void kategorieChangeListener(AjaxBehaviorEvent e) {
        System.out.println("kategoriename: " + kategorie.getName());
    }

KategorieConverter:

package xxx.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import xxx.bean.KategorieBean;
import xxx.hibernate.entity.Kategorie;

@FacesConverter(forClass = Kategorie.class)
public class KategorieConverter implements Converter{

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        System.out.println("Converter: getAsObject");
        KategorieBean kB = (KategorieBean) FacesContext.getCurrentInstance().
                getExternalContext().getSessionMap().get("kategorieBean");
        for (Kategorie k : kB.getAllKategories()){
            if (k.getKategorieId() == Long.valueOf(value))
                return k;
        }
        return null;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        System.out.println("Converter: getAsString");
        return String.valueOf(((Kategorie) value).getKategorieId());
    }
}

Console output (inital):

  • Hibernate: select this_.kategorie_id as kategori1_1_0_, this_.beschreibung as beschrei2_1_0_, this_.liferay_company_id as liferay3_1_0_, this_.liferay_group_id as liferay4_1_0_, this_.name as name5_1_0_, this_.parent_id as parent6_1_0_ from usushop_kategorie this_
  • getter kategorie: kategorie=null
  • Converter: getAsString
  • Converter: getAsString
  • Converter: getAsString
  • Converter: getAsString
  • Converter: getAsString
  • Converter: getAsString
  • getter kategorie: kategorie=null

Console output after value in the selectOneMenu changed:

  • Converter: getAsObject
  • getter kategorie: kategorie=null
  • setter kategorie: name=Kategorie4
  • kategoriename: Kategorie4
  • getter kategorie: name=Kategorie4
1

1 Answers

1
votes

You can use Richface's a4j(ajax4jsf)

<%@taglib prefix="a4j" uri="http://richfaces.org/a4j"%>



<h:form>
    <h:selectOneMenu  value="#{kategorieBean.kategorie}">
            <f:selectItem
                            itemLabel="Wählen..."
                            itemValue="#{null}"/>
            <f:selectItems 
                            value="#{kategorieBean.allKategories}"
                            var="k" 
                            itemLabel="#{k.name}"
                            itemValue="#{k}" />     
            <a4j:support event="onchange" action="#{bean.SomeAction}" reRender="result"/>
        </h:selectOneMenu>
<h:panelGroup id="result">
    <h:outputText  value="#{kategorieBean.kategorie.name}" />
</h:panelGroup>
</h:form>