【问题标题】:ValueChangeListener not workingValueChangeListener 不工作
【发布时间】:2011-04-05 10:57:35
【问题描述】:

这是我在托管 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 完全没用。我还是想知道它是如何工作的。

提前致谢:)

【问题讨论】:

    标签: java jsf jsf-2 valuechangelistener


    【解决方案1】:

    我不确定您所说的“价值没有得到”是什么意思。你应该再澄清一点。

    至少,它的唯一目的是侦听值更改,以便您可以根据更改执行一些业务操作,例如记录或预加载与新值相关的一些内容。当初始值等于提交值时,则不会调用该方法。

    这里有一个代码 sn-p 可以自己玩弄:

    <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。您实际上显示的是托管 bean 的值,而不是 f:valueChangeListener 新创建的 bean 的值。

    【讨论】:

    • 嗨 BalusC,请阅读我编辑的帖子。我没有使用组件的 valuelistener。我正在使用f:valueListener。你能在我的代码中找到错误吗?我正在阅读这篇文章 :- java-server-faces.blogspot.com/2006/04/… 但它不起作用:(
    • 查看更新。要解决这个问题,请继续我提出的方法。
    • 嗨 BalusC,我没听懂。那么接下来我要做什么呢?
    • f:valueChangeListener 创建 bean 的 new 实例。它没有使用与输入/输出组件显示的相同实例。您现在有两个 bean,并且您正在显示来自“错误”bean 的值。使用我的方法来实现您最初想到的要求。使用您的方法从托管 bean 中完全分离监听更改事件(并且您对访问视图中的值更改监听器 bean 不感兴趣)。
    • 太棒了!谢谢BalusC。顺便说一句,您的积分大大增加了!上次我在 6 月份使用 JSF 2.0 时,它们是 54.4k。现在它们是 82.8k。这样你甚至可以打破 Jon Skeet 的记录。
    猜你喜欢
    • 2011-12-18
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2014-07-01
    • 2012-10-18
    • 1970-01-01
    • 2013-05-09
    • 2013-05-02
    相关资源
    最近更新 更多