如何从< h:selectOneMenu>获取价值? [英] how to get value from <h:selectOneMenu>?

查看:87
本文介绍了如何从< h:selectOneMenu>获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从获取值.我尝试了这段代码,但没有用:

i want to get the value from . i tryed this code but it doesn't work :

<h:form>
        <h:outputLabel value="départements : "/>
        <h:selectOneMenu value="#{departementController.selected.id}" onchange="submit()" immediate="true">
            <f:valueChangeListener type="Controller.Listener.DepartementValueListener"/>
            <f:selectItems value="#{departementController.itemsAvailableSelectOne}"/>
        </h:selectOneMenu>

        <h:outputLabel value="nouvelle valeur : "/>

        <h:inputText value="#{departementController.comboBox}" id="dep"/>
    </h:form>

控制器:

 //departement change listener
private String comboBox;

public String getComboBox() {
    return comboBox;
}

public void setComboBox(String comboBox) {
    this.comboBox = comboBox;
}

public void departementChangeListener(ValueChangeEvent e) {
    // Skip validation of non-immediate components and invocation of the submit() method.
    FacesContext.getCurrentInstance().renderResponse();
    this.comboBox = e.getNewValue().toString();}

sourceId = j_idt7:j_idt9 [severity =(ERROR 2),summary =(j_idt7:j_idt9 )]

sourceId=j_idt7:j_idt9[severity=(ERROR 2), summary=(j_idt7:j_idt9 : erreur de validation. La valeur est incorrecte.), detail=(j_idt7:j_idt9 : erreur de validation. La valeur est incorrecte.)]

推荐答案

您有两个问题.

首先,错误错误验证.无效的错误" 验证错误:值无效" 的法语翻译,表示已提交值不是<f:selectItems>中的任何可用项目.您的代码不够完整,无法指出根本原因,但是我猜测,您在<f:selectItems value>中有一个List<Department>,因此每个项目都是Department,但是您正在尝试将其设置为idString值,而不是设置为Department.这个不对.您需要在DepartmentString之间提供一个转换器,并改用#{departementController.selected}.

First, the error "erreur de validation. La valeur est incorrecte" which is the French translation of "Validation Error: Value is not valid" means that the submitted value doesn't equals() any one of the available items in <f:selectItems>. Your code is not complete enough to point out the root cause, but I guess that you've a List<Department> there in <f:selectItems value> and thus every item is Department, but you're trying to set it as a String value of id instead of as Department. This is not right. You need to supply a converter between Department and String and use #{departementController.selected} instead.

类似这样的东西:

<h:selectOneMenu value="#{bean.selectedDepartment}">
    <f:selectItems value="#{bean.availableDepartments}" />
</h:selectOneMenu>

使用

private Department selectedDepartment;
private List<Department> availableDepartments;

还有一个@FacesConverter,它可以在Department及其唯一的String表示形式之间进行转换.

And a @FacesConverter which converts between Department and its unique String representation.

您的第二个问题是,您似乎对JSF 1.x目标示例过于关注,而不是在下拉列表的更改中填充另一个字段.您正在为此使用笨拙/笨拙的JSF 1.x解决方法.在JSF 2.x中,您可以为此使用<f:ajax>.

Your second problem is that you seem to be focusing too much on JSF 1.x targeted examples as to populating another field on change of the dropdown. You're using a rather clumsy/hacky JSF 1.x workaround for that. In JSF 2.x you can just use <f:ajax> for this.

<h:selectOneMenu value="#{bean.selectedDepartment}">
    <f:selectItems value="#{bean.availableDepartments}" />
    <f:ajax listener="#{bean.changeDepartment}" render="inputId" />
</h:selectOneMenu>
<h:inputText id="inputId" value="#{bean.input}" />

使用

public void changeDepartment() {
    input = selectedDepartment.getId();
}

另请参见:

  • 我们的selectonemenu Wiki页面
  • See also:

    • Our selectonemenu wiki page
    • 这篇关于如何从&lt; h:selectOneMenu&gt;获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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