获取SelectOneMenu的选定值

获取SelectOneMenu的选定值

本文介绍了获取SelectOneMenu的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在jsf页面上测试组件"SelectOneMenu".我正在通过我的ManageBean动态填充该组件(将从数据库中获取所有Animals).

I'm testing the component "SelectOneMenu" on a jsf page. I'm populating this component dinamically though my ManageBean (that will get all Animals from database).

我想知道是否可以看到用户选择的"SelectOneMenu"(组合框)项目,但我正在尝试使用value =#{animalsManage.animalSelect}",但仅在这一页.另外,我正在使用inputText来查看"SelectOneMenu"的所选intem的值.

I would like to know if is possible to see the user selected item of that "SelectOneMenu" (combobox), I'm trying with value="#{animalsManage.animalSelect}" but it is only called on the beginning of the page. Also, I'm using an inputText to see the value of the selected intem of the "SelectOneMenu".

我做错了什么?

JSF:

    <body>
    <ui:component>
        <h:form>
                    <h:outputText value="Select one Mets File" />
                    <h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
                        <f:selectItem itemLabel="Select..."  noSelectionOption="true"/>
                        <f:selectItems value="#{animalsManage.allAnimals}" />
                    </h:selectOneMenu>
                    <h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
        </h:form>
    </ui:component>
</body>

ManageBean:

ManageBean:

    @ManagedBean
    @ViewScoped
    public class AnimalsManage implements Serializable {

    @EJB
    private AnimalsFacadeREST animalsFacadeREST;
    private String animalSelected;
    private List< SelectItem> selectAnimals;

    public List<SelectItem> getAllAnimals() {
            List<Animals> al = animalsFacadeREST.findAll();
            selectAnimals = new ArrayList< SelectItem>();
            int i = 0;
            for (Animals animal: al) {
                selectAnimals.add(new SelectItem(i, animal.getName()));
                i++;
            }
            return selectAnimals;
    }

    public String getAnimalSelected() {
       return animalSelected;
    }

    public void setAnimalSelected(String animalSelected) {
        this.animalSelected = animalSelected;
    }
}

推荐答案

存在许多解决此问题的方法.我在这里提出两个基本想法.

There are many solutions to the presented problem. I present here two basic ideas.

  1. 服务器端解决方案.只需在<h:selectOneMenu>内附加<f:ajax>标记即可更新选定的值并重新呈现用户的选择,例如

  1. Server-side solution. Simply attach <f:ajax> tag inside your <h:selectOneMenu> to update selected values and rerender user's choice, like in

<h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
    <f:selectItem itemLabel="Select..."  noSelectionOption="true"/>
    <f:selectItems value="#{animalsManage.allAnimals}" />
    <f:ajax execute="combo" render="textbox" />
</h:selectOneMenu>
<h:inputText id="textbox" value="#{animalsManage.animalSelected }" />

如果愿意,还可以通过指定<f:ajax>标签的listener="#{animalsManage.performCustomAjaxLogic}"对ajax侦听器中的选定元素进行一些自定义逻辑.

If you like, you may also do some custom logic with selected element in ajax listener by specifying listener="#{animalsManage.performCustomAjaxLogic}" of <f:ajax> tag.

客户端解决方案.只需在基本更改事件上使用id ="textbox"更新元素.因此,如果您使用jQuery,则解决方案将是

Client-side solution. Simply update element with id="textbox" on basic change event. So, if you use jQuery the solution will be

$('#combo').change(function() {
    $('#textbox').val($('#combo').val());
});

认为客户端解决方案将仅绑定输入组件的文本值.

Thought the client-side solution will bind only text value of your input component.

这篇关于获取SelectOneMenu的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:54