2010-08-29 3 views
2

Ceci est mon code dans mon bean géré: -ValueChangeListener ne fonctionne pas

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <title>Facelet Title</title> 
    </h:head> 
    <h:body> 
     <center> 
      <h:form> 
       <h2> <u>Select Fruit(s). </u> </h2> 

       <!-- Value Change Listener is entirely superflous. You can make a full blown project without requiring the need to ever use this...This is 
       just for demo purpose..--> 

       <h:selectManyMenu onchange="document.forms[0].submit();" style="height: 200px;font-size: 1.5em;width: 200px;" > 
        <f:selectItems value="#{actionValueLisBean.fruitsList}" var="fruit" itemLabel="#{fruit}" itemValue="#{fruit}"/> 

        <f:valueChangeListener type="beans.ActionValueLisBean"/> 
       </h:selectManyMenu> 

       <h3> Your previous selection is :<h:outputText value="#{actionValueLisBean.prevSel}"/></h3> 
       <h3>Your current selection is :<h:outputText value="#{actionValueLisBean.currSel}"/></h3> 

      </h:form> 
     </center> 
    </h:body> 
</html> 

Ceci est mon haricot: -

package beans; 

import com.sun.jmx.remote.internal.ArrayQueue; 
import java.util.List; 
import javax.enterprise.context.RequestScoped; 
import javax.faces.bean.ManagedBean; 
import javax.faces.event.AbortProcessingException; 
import javax.faces.event.ValueChangeEvent; 
import javax.faces.event.ValueChangeListener; 

@ManagedBean 
@RequestScoped 
public class ActionValueLisBean implements ValueChangeListener { 


    private List<String> fruitsList; 
    private String currSel; 
    private String prevSel; 

    public String getCurrSel() { 
     return currSel; 
    } 

    public void setCurrSel(String currSel) { 
     this.currSel = currSel; 
    } 

    public String getPrevSel() { 
     return prevSel; 
    } 

    public void setPrevSel(String prevSel) { 
     this.prevSel = prevSel; 
    } 



    public List<String> getFruitsList() { 
     return fruitsList; 
    } 

    public void setFruitsList(List<String> fruitsList) { 
     this.fruitsList = fruitsList; 
    } 



    public ActionValueLisBean() { 
     fruitsList = new ArrayQueue<String>(5); 

     fruitsList.add("Apple"); 
     fruitsList.add("Mango"); 
     fruitsList.add("Banana"); 
     fruitsList.add("Peach"); 
     fruitsList.add("Plum"); 

    } 

    @Override 
    public void processValueChange(ValueChangeEvent event) throws AbortProcessingException { 

      if(event.getOldValue() != null) 
       prevSel = event.getOldValue().toString(); 
      if(event.getNewValue() != null) 
       currSel = "abc"; 

    } 


} 

Et puis j'ai prevSel et currSel est lié à h:outputText. Mais la valeur n'obtient pas même si processValueChange est déclenché correctement et System.out.println(event.getNewValue()); fonctionne également correctement. J'ai essayé de définir différentes étendues de haricots, mais pas d'utilisation. Je sais que ValueChangeListener est totalement inutile. Encore je veux savoir comment cela fonctionne.

Merci à l'avance :)

Répondre

2

Je ne suis pas sûr de ce que vous entendez par « la valeur ne reçoit pas ». Vous devriez clarifier un peu plus. Au moins, son seul but est d'écouter sur un changement de valeur afin que vous puissiez faire quelques affaires basées sur le changement comme la journalisation ou le préchargement de certains éléments liés à la nouvelle valeur. Lorsque la valeur initiale est égale à la valeur soumise, cette méthode ne sera pas appelée.

Voici un extrait de code pour jouer avec vous-même:

<h:form> 
    <p>Input value: <h:inputText value="#{bean.value}" valueChangeListener="#{bean.change}"/></p> 
    <p>Old value: <h:outputText value="#{bean.oldValue}" /></p> 
    <p>New value: <h:outputText value="#{bean.newValue}" /></p> 
    <p><h:commandButton value="submit" action="#{bean.submit}" /></p> 
</h:form> 

Bean:

package com.example; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.event.ValueChangeEvent; 

@ManagedBean 
@ViewScoped 
public class Bean { 

    private String value; 
    private String oldValue; 
    private String newValue; 

    public void submit() { 
     System.out.println("Submit: " + value); 
    } 

    public void change(ValueChangeEvent event) { 
     oldValue = (String) event.getOldValue(); 
     newValue = (String) event.getNewValue(); 
     System.out.println("Change: " + oldValue + " to " + newValue); 
    } 

    public String getValue() { 
     return value; 
    } 

    public String getOldValue() { 
     return oldValue; 
    } 

    public String getNewValue() { 
     return newValue; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 

} 

Mise à jour: selon votre mise à jour: vous utilisez deux différents instances du haricot. Vous affichez en fait les valeurs du bean géré, pas du bean nouvellement créé par f:valueChangeListener.

+0

Salut BalusC, s'il vous plaît lire mon article édité. Je n'utilise pas valuelistener de composant. J'utilise 'f: valueListener'. Pouvez-vous trouver un bug dans mon code? Je lis cet article: - http://java-server-faces.blogspot.com/2006/04/valuechangelisteners-what-you-need-to.html Mais ça ne marche pas :( – TCM

+0

Voir le Pour corriger ce problème, poursuivez avec l'approche que vous avez proposée: – BalusC

+0

Salut BalusC, je n'ai pas compris ... Alors, que dois-je faire ensuite? – TCM