java ValueChangeListener 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3595421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:34:02  来源:igfitidea点击:

ValueChangeListener not working

javajsfjsf-2valuechangelistener

提问by TCM

This is my code in my managed bean :-

这是我在托管 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>

This is my bean :-

这是我的豆子:-

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

    }


}

And then i have prevSel and currSel is bound to h:outputText. But the value is not getting even though the processValueChange is fired correctly and System.out.println(event.getNewValue());is also working fine. I tried setting different scopes of bean too but no use. I know ValueChangeListener is entirely useless. Still i want to know how it works.

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

Thanks in advance :)

提前致谢 :)

采纳答案by BalusC

I am not sure what you mean with "the value is not getting". You should clarify that bit more.

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

At least, its sole purpose is to listen on a value change so that you can do some business stuff based on the change such as logging or preloading some stuff related to the new value. When the initial value equals to the submitted value, then this method won't be invoked.

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

Here's a code snippet to play around with it yourself:

这是一个代码片段,您可以自己玩弄它:

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

}


Update: as per your update: you're using two different instancesof the bean. You're actually displaying the values of the managed bean, not of the bean which is newly created by f:valueChangeListener.

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