JSF 2.0:如何动态生成输入组件
在我的应用程序中,我有以下 Constants 类
public class Constants {
...
public static final int MAX_NUM_OF_PICTURES = 2
...
}
早些时候,当我使用 JSP 时,我设法根据此常量动态呈现用于上传文件的输入字段,如下所示:
<%
for (int i = 1; i < Constants.MAX_NUM_OF_PICTURES + 1; i++) {
%>
<tr>
<td>Upload Picture <%= i %></td>
<td><input name="<%= i%>" type="file" /></td>
</tr>
<tr>
<td>Description <%= i %></td>
<td><input type="text" name="<%= "description" + i%>" id="description" /></td>
</tr>
<%
}
%>
目前,我正在尝试使用 JSF 来实现上述任务。如果这些输入字段不是动态生成的,我可以轻松地在支持 bean 中定义以下属性:
@ManagedBean
@RequestScoped
public class MrBean {
...
private UploadedFile picture1;
private String pictDescription1;
...
}
但是,由于这些字段现在是动态生成的,我无法知道需要提前定义多少属性来捕获这些上传的文件。
如果有人能给我建议如何解决这个问题,我将不胜感激?
最好的问候,
詹姆斯·特兰
In my application, I have the following Constants class
public class Constants {
...
public static final int MAX_NUM_OF_PICTURES = 2
...
}
Earlier when I was using JSP, I managed to dynamically render input fields for uploading files based on this constant as following:
<%
for (int i = 1; i < Constants.MAX_NUM_OF_PICTURES + 1; i++) {
%>
<tr>
<td>Upload Picture <%= i %></td>
<td><input name="<%= i%>" type="file" /></td>
</tr>
<tr>
<td>Description <%= i %></td>
<td><input type="text" name="<%= "description" + i%>" id="description" /></td>
</tr>
<%
}
%>
Currently, I am trying to use JSF to achieve the above task. If these input fields are not dynamically generated, I can easily define the following properties in my backing bean:
@ManagedBean
@RequestScoped
public class MrBean {
...
private UploadedFile picture1;
private String pictDescription1;
...
}
However, since these fields are now dynamically generated, I cannot know how many properties I would need to define in advance to capture these uploaded files.
I'd be very grateful if someone could give me an advice on how I should tackle this problem?
Best regards,
James Tran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将这些属性放在另一个 javabean 类中,并在托管 bean 中拥有这些 javabean 的集合。
例如
,
然后您可以在
中循环它(或者可能
,但是如果您想要两个重复的行而不是一个)。我不知道你使用什么组件库来上传文件,所以我假设它只是 Tomahawk。
Put those properties in another javabean class and have a collection of those javabeans in your managed bean.
E.g.
and
Then you can loop over it in for example
<ui:repeat>
(or maybe<h:dataTable>
, but this isn't really suitable if you want two repeating rows instead of one).I have no idea what component library you're using for uploading files, so I assumed it be just Tomahawk.