ValueChangeListener不起作用

时间:2010-08-29 15:49:41

标签: java jsf jsf-2 valuechangelistener

这是我在托管bean中的代码: -

<?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>

这是我的豆子: -

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";

    }


}

然后我有prevSel和currSel绑定h:outputText。但即使正确触发processValueChange并且System.out.println(event.getNewValue());也正常工作,该值也无法获得。我尝试设置不同的bean范围但没有用。我知道ValueChangeListener完全没用。我还想知道它是如何运作的。

提前致谢:)

1 个答案:

答案 0 :(得分:2)

我不确定你的意思是“价值没有得到”。你应该多说清楚。

至少,它的唯一目的是监听价值变化,以便您可以根据变化做一些业务,例如记录或预加载与新值相关的一些东西。当初始值等于提交的值时,将不会调用此方法。

这是一个自己玩的代码片段:

<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>

豆:

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;
    }

}

更新:根据您的更新:您使用的是两个不同的实例。您实际上是显示托管bean的值,而不是f:valueChangeListener新创建的bean的值。