JSF ValueChangeListener不起作用 [英] JSF ValueChangeListener not working

查看:74
本文介绍了JSF ValueChangeListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在JSF中使用事件.由于某些原因,当我更改下拉菜单的值时,程序没有更改文本字段中的值.该页面将加载并显示德国",但不会将文本字段更改为"DE".有什么建议吗?

I am learning how to use events with JSF. For some reason, the program is not changing the value in the text field when I change the value of the dropdown menu. The page loads and shows "Germany", but does not change the text field to "DE". Any suggestions?

Index.xhtml:

Index.xhtml:

<?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:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <f:view>
        <h:form>
            <h:selectOneMenu value="#{Bean.selectedItem}" valueChangeListener="#{Bean.changeEvent}" onchange="submit()"  >
                <f:selectItems value="#{Bean.itemsList}" />
            </h:selectOneMenu>
            <br />
            <h:inputText value="#{Bean.selectedItem}" />
        </h:form>
    </f:view>
</h:body>

MyBean.java

MyBean.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.ArrayList;
import javax.faces.model.SelectItem;
import javax.faces.event.ValueChangeEvent;
@ManagedBean (name = "Bean")
@RequestScoped
public class MyBean {

private static ArrayList menuItems;
private String selectedItem = "EN";
static {
    menuItems = new ArrayList();
    menuItems.add(new SelectItem("EN", "English"));
    menuItems.add(new SelectItem("DE", "Germany"));
}

public ArrayList getItemsList() {
    return this.menuItems;
}

public void setSelectedItem(String item) {
    this.selectedItem = item;
}

public String getSelectedItem() {
    return selectedItem;
}

public void changeEvent(ValueChangeEvent e) {
    selectedItem = e.getNewValue().toString();
}

}

推荐答案

您基本上是在滥用valueChangeListener作为actionListener.值更改侦听器旨在在提交新输入值和更新模型值之间,同时访问两者新旧值.因此,例如,您可以添加有关值更改的日志条目.您显然对旧值不感兴趣.您根本对价值变更事件不感兴趣.您只对新模型值感兴趣.因此,您根本不应该使用值更改侦听器.

You're basically abusing the valueChangeListener as an actionListener. The value change listener is intented to have access to both the old and new value, right in between when the new input value is been submitted and the model value is been updated, so that you can for example add a log entry about the value change. You are apparently not interested in the old value. You're not interested in the value change event at all. You're only interested in the new model value. So you shouldn't be using the value change listener at all.

通常,您希望使用动作侦听器方法来完成此工作.您可以使用<f:ajax>定义ajax操作侦听器:

Normally, you'd like to do this job in an action listener method. You can use <f:ajax> to define an ajax action listener:

<h:selectOneMenu value="#{Bean.selectedItem}">
    <f:selectItems value="#{Bean.itemsList}" />
    <f:ajax execute="@this" listener="#{Bean.changeEvent}" render="input" />
</h:selectOneMenu>
<h:inputText id="input" value="#{Bean.selectedItem}" />

使用

public void changeEvent() {
    selectedItem = selectedItem;
}

但是由于您已将输入绑定到与下拉列表相同的属性,因此整个侦听器都是多余的.请执行以下操作:

But since you've bound the input to the same property as the dropdown, the whole listener is superfluous. Just the following should do it for you:

<h:selectOneMenu value="#{Bean.selectedItem}">
    <f:selectItems value="#{Bean.itemsList}" />
    <f:ajax execute="@this" render="input" />
</h:selectOneMenu>
<h:inputText id="input" value="#{Bean.selectedItem}" />

您看到的问题是因为onchange="submit()"本质上是提交 whole 表单,包括所有其他输入字段.当JSF按顺序处理输入字段并将两个字段绑定到相同的属性时,<h:inputText>的值将覆盖<h:selectOneMenu>的值.根据我对这个问题的评论,当您将它设为<h:inputText readonly="true"><h:outputText>时,您将不会遇到此问题(这样它就不会将其值提交给服务器).

The problem which you're seeing is because the onchange="submit()" essentially submits the whole form, including all other input fields. As JSF processes input fields in sequence and you've bound the both fields to the same property, the value of <h:inputText> will override the value of <h:selectOneMenu>. As per my comment on the question, you would not have this problem when you made it a <h:inputText readonly="true"> or a <h:outputText> (so that it won't submit its value to the server).

这篇关于JSF ValueChangeListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆