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

编程入门 行业动态 更新时间:2024-10-28 00:29:53
本文介绍了如何将多个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]}" />

或将<ui:repeat>与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>

不要不要使用var属性,如

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

提交表单时它将不起作用,因为String没有值的设置方法(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:32:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数组   如何将   属性   inputText

发布评论

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

>www.elefans.com

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