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

问题描述

我需要一些帮助.我正在为 jsf primefaces 网络应用程序进行开发,当我从下拉列表中选择以获取所选值时遇到了问题在操作中得到一个空字符串.

I need some help. I'm developing for jsf and primefaces web application and I'm facing a problem when I'm selecting from a drop down list to get the selected value but I'm getting an empty string in the action.

这是我的selectOneMenu标记的xhtml代码

This is my xhtml code for selectOneMenu tag

<p:selectOneMenu value="#{tanAllot.batchName}" id="batchName">
    <f:selectItem itemLabel="Select Batch" itemValue="" />
    <f:selectItems value="#{tanAllot.batchList}" />
    <p:ajax event="change" listener="#{tanAllot.test}" />
</p:selectOneMenu>

这是我在动作类中使用的方法

this is the method I'm using in the action class

private String batchName;

public String getBatchName() {
    return batchName;
}

public void setBatchName(String batchName) {
    this.batchName = batchName;
}

public void test() {
    System.out.println(batchName);
}

我的问题是,当我从p:selectOneMenu标记中选择一个值时,默认方法应在操作中调用并检索该值,但我得到的是空字符串.

My problem is when I select a value from p:selectOneMenu tag the default method should invoke in the action and retrieve the value but I'm getting an empty string.

有人可以帮我解决这个问题吗?

Can anyone help me to solve this problem?

推荐答案

请考虑batchList的性质. batchList必须是字符串列表.使用itemValue属性(位于f:selectItem中)可能会有所帮助.

Consider the nature of batchList. batchList must be List of Strings. Using itemValue attribute (in f:selectItem) can be helpful.

检查我的示例.它使用省份列表(省"类的实例).但是,我只需要"id"值,即"Long";如果我希望将整个选择的省作为省对象",则需要一个转换器". (示例效果很好):

Check my example. It uses a list of provinces (instances of "Province" class). However I only need the "id" value which is a "Long"; if I wanted the whole picked province as a "Province object" I would need a "Converter". (Example works perfectly):

<p:selectOneMenu id="provinceField"
        value="#{addAddressesMB.formAddress.provinceId}">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{addAddressesMB.provinceList}" var="i"
        itemLabel="#{i.description}" itemValue="#{i.id}" />
        <p:ajax update=":formId:cityField"
            listener="#{addAddressesMB.provinceChangeHandler}" />
</p:selectOneMenu>

这是侦听器方法:

public void provinceChangeHandler() {
    //do whatever you want with formAddress.provinceId
    System.out.println(this.formAddress.provinceId);
    //In my case I filter the cities according to the selected provinceId
    // After that I update the cities dropdown(cityField) in the view.
}

检查您的代码,随时询问.祝你好运.

Check your code and feel free to ask. Good luck.

这篇关于primefaces selectitem onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:15