如何将多个 inputText 映射到数组属性?

编程入门 行业动态 更新时间:2024-10-28 02:28:43
本文介绍了如何将多个 inputText 映射到数组属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望用户为 JSF 的 inputText 组件输入一个或多个名称.所以我在考虑这样的托管 bean:

I want the user to enter one or more names to the JSF's inputText components. So I'm thinking of a managed bean like this:

public class MyBean { private String[] names; public String[] getNames() { return names; } public void setNames(String[] names) { this.names = names; } }

但是,如何将 JSF 的 inputText 组件映射到此数组属性?

But, how do I map the JSF's inputText components to this array property?

推荐答案

首先,您需要在 bean 的(后)构造函数中保留数组.例如

First, you need to preserve the array in bean's (post)constructor. E.g.

public MyBean() { names = new String[3]; }

然后,您可以或者通过硬编码索引访问它们

Then, you can either just access them by an hardcoded index

<h:inputText value="#{myBean.names[0]}" /> <h:inputText value="#{myBean.names[1]}" /> <h:inputText value="#{myBean.names[2]}" />

或使用 和 varStatus 来通过动态索引访问它们

or use <ui:repeat> with a varStatus to access them by a dynamic index

<ui:repeat value="#{myBean.names}" varStatus="loop"> <h:inputText value="#{myBean.names[loop.index]}" /> </ui:repeat>

不要不要像

<ui:repeat value="#{myBean.names}" var="name"> <h:inputText value="#{name}" /> </ui:repeat>

提交表单时不起作用,因为 String 没有值的 setter(getter 基本上是 toString() 方法).

It won't work when you submit the form, because String doesn't have a setter for the value (the getter is basically the toString() method).

更多推荐

如何将多个 inputText 映射到数组属性?

本文发布于:2023-10-23 03:31:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数组   如何将   属性   inputText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!