【问题标题】:Dropdown list is not populating in JSFJSF 中未填充下拉列表
【发布时间】:2022-01-19 04:58:39
【问题描述】:

我正在研究 Managedbeans 和 JSF。如下所示,我的 ManagedBean 包含 JSF 获取值所需的所有要求。我已经初始化了我的下拉列表,如下所示。在 selectOneMenu 中,我选择了国家作为字符串,它将存储下拉列表选择的值,下拉列表将显示我在 Beans 中声明的列表。 不幸的是,它不是那样发生的。每次下拉呈现它都会给我一个空值。我已经花了几天时间,但无法找出确切的解决方案。我已经清理了我的服务器,构建了工作空间并更改了服务器,但没有任何效果。

** ManagedBean_list **

private List<String> listCountry;   
    private String country;
        public void tada(){
        listCountry=Arrays.asList("India", "pakisatan","America");
    }   
    public List<String> getListCountry() {
        return listCountry;
    }
    public void setListCountry(List<String> listCountry) {
        this.listCountry = listCountry;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

JSF

<p:selectOneMenu id="country" value="#{loginBeans.country}">
<f:selectItems value="#{loginBeans.listCountry}"  />
</p:selectOneMenu>

感谢您的帮助。 Empty dropdown list image enter image description here

【问题讨论】:

  • 请出示ManagedBean的完整代码。
  • 排除显而易见的问题:请再次确认您已在尝试使用&lt;f:selectItems&gt; 标记的同一个XHTML 文件中正确声明xmlns:f="..."。有点理智的 IDE 应该已经在那里显示了一条警告线。
  • 您好,感谢您的帮助。好吧,问题不在于标签库或 bean。它在列表函数的初始化中。我通过在托管 bean 类的构造函数中包含列表函数来解决它。这样当构造函数启动时。它还会生成下拉列表。

标签: jsf primefaces dropdown javabeans selectonemenu


【解决方案1】:

您使用的是哪个 bean 注释?您说“Managedbeans”,但您发布的源代码并未显示整个 bean,或者是这样吗?检查以确保您没有将旧式 JSF 托管 bean 注释与 CDI 注释混合。

【讨论】:

  • 这不太可能是问题所在。虽然确实效率低下,但默认 JSF 托管 bean 范围以及默认 CDI 托管 bean 范围足以让 加载。
  • 是的,你是对的,在互联网上,我们大多看到这样的例子,但在我的例子中,它不起作用,所以我在构造函数中调用它以使其工作。
【解决方案2】:

问题是在初始化时,列表没有被调用。我通过在托管 bean 类的构造函数中包含列表函数来解决它。这样当构造函数启动时。它还会生成下拉列表。

【讨论】:

    【解决方案3】:

    将您的listCountry 转换为

        private Map<String, String> listCountry = new HashMap<>();
        listCountry.put("India", "India");
        listCountry.put("Pakistan", "Pakistan");
        listCountry.put("America", "America");
    

    private List<SelectItem> listCountry = new ArrayList<>();
    listCountry.add(new SelectItem("India", "India"));
    listCountry.add(new SelectItem("Pakistan", "Pakistan"));
    listCountry.add(new SelectItem("America","America"));
    

    【讨论】:

    • 不,它在我的情况下不起作用。
    • 下次尝试自己重现问题。您会注意到模型的原始代码运行良好(前提是视图的代码是正确的)。
    猜你喜欢
    • 2020-06-11
    • 2014-10-14
    • 2020-06-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多