如何将集合元素显示为 selectManyListbox 中的项目?
我有一个 bean:
public ProjectServiceImpl {
public List<Project> getAllProjects () { ... }
}
我想将所有这些项目作为
中的项目列出。当用户选择一个或多个项目并按下提交按钮时,所选项目应转换为项目。
我对如何列出项目以及对应的转换器应该是什么样子有点困惑?
I have a bean:
public ProjectServiceImpl {
public List<Project> getAllProjects () { ... }
}
I want to list all these projects as items in <h:selectManyListbox>
. When user selects one or more items and press submit button, selected items should be converted into projects.
I'm confused a little about how to list items and how correspondent converter should look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要实现
Converter#getAsString()
以便所需的 Java 对象以 <可以用作HTTP请求参数的strong>唯一字符串表示形式。使用数据库技术 ID(主键)在这里非常有用。然后你需要实现
Converter#getAsObject()
以便 HTTP 请求参数(根据定义String
) 可以转换回所需的 Java 对象(在您的情况下为Project
)`。最后,只需将此转换器与相关对象类型相关联,JSF 将在
Project
出现时处理转换,无需指定converterId
或f:converter
:这样您就可以使用
Project
作为值创建SelectItem
。您可以从这篇博客文章中获取一些背景信息和更多想法:http ://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html
You need to implement
Converter#getAsString()
so that the desired Java Object is been represented in an unique string representation which can be used as HTTP request parameter. Using the database technical ID (the primary key) is very useful here.Then you need to implement
Converter#getAsObject()
so that the HTTP request parameter (which is per definitionString
) can be converted back to the desired Java Object (Project
in your case)`.Finally just associate this converter with the object type in question, JSF will then take care about conversion when
Project
comes into the picture no need to specify aconverterId
or af:converter
:This way you can just create
SelectItem
withProject
as value.You can get some background info and more ideas out of this blog article: http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html
要列出
中的项目,您需要使用
并将值指向SelectItem
列表> 对象。我通常处理此问题的方法是循环遍历项目并将每个项目转换为
SelectItem
。同时,我还使用SelectItem
值作为键将项目存储在HashMap
中。然后,当您需要获取项目对象列表时,您可以循环遍历所选值并从地图中抓取对象。如果不想创建
HashMap
,可以使用Project
在List中的位置作为SelectItem
值,并查找那样的项目。To list items in a
<h:selectManyListbox>
you need to use<f:selectItems>
and point the value at a list ofSelectItem
objects.The way I normally approach this is to loop through the projects and convert each project into a
SelectItem
. At the same time, I also store the projects in aHashMap
using theSelectItem
value as the key. Then when you need to get the list of project objects, you can loop through the selected values and grab the objects from the map.If you do not want to create the
HashMap
, you can use the position of theProject
in the List as theSelectItem
value and look up the projects that way.