如何将集合元素显示为 selectManyListbox 中的项目?

发布于 2024-08-17 15:38:06 字数 287 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-08-24 15:38:06

您需要实现Converter#getAsString() 以便所需的 Java 对象以 <可以用作HTTP请求参数的strong>唯一字符串表示形式。使用数据库技术 ID(主键)在这里非常有用。

public String getAsString(FacesContext context, UIComponent component, Object value) {
    // Convert the Project object to its unique String representation.
    return String.valueOf(((Project) value).getId());
}

然后你需要实现 Converter#getAsObject() 以便 HTTP 请求参数(根据定义String) 可以转换回所需的 Java 对象(在您的情况下为 Project)`。

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    // Convert the unique String representation of Project to the actual Project object.
    return projectDAO.find(Long.valueOf(value));
}

最后,只需将此转换器与相关对象类型相关联,JSF 将在 Project 出现时处理转换,无需指定 converterIdf:converter

<converter>
    <converter-for-class>com.example.Project</converter-for-class>
    <converter-class>com.example.ProjectConverter</converter-class>
</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.

public String getAsString(FacesContext context, UIComponent component, Object value) {
    // Convert the Project object to its unique String representation.
    return String.valueOf(((Project) value).getId());
}

Then you need to implement Converter#getAsObject() so that the HTTP request parameter (which is per definition String) can be converted back to the desired Java Object (Project in your case)`.

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    // Convert the unique String representation of Project to the actual Project object.
    return projectDAO.find(Long.valueOf(value));
}

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 a converterId or a f:converter:

<converter>
    <converter-for-class>com.example.Project</converter-for-class>
    <converter-class>com.example.ProjectConverter</converter-class>
</converter>

This way you can just create SelectItem with Project 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

吝吻 2024-08-24 15:38:06

要列出 中的项目,您需要使用 并将值指向 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 of SelectItem 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 a HashMap using the SelectItem 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 the Project in the List as the SelectItem value and look up the projects that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文