| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 667 人关注过本帖
标题:JSF : 将Collection集合内容放入selectOneMenu组件
只看楼主 加入收藏
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
 问题点数:0 回复次数:0 
JSF : 将Collection集合内容放入selectOneMenu组件
比如我们有如下的Plate类
package com.bbpop.entity;

import javax.persistence.*;

/**
*
* @author vlinux
*/
@Entity
@Table(name = "PLATE")
public class Plate implements java.io.Serializable {

public Plate() {
this.locked = false;
this.hidden = false;
}

public Plate(String title, String comment) {
this();
this.title = title;
this.comment = comment;
}


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;

@Column(name = "TITLE", columnDefinition = "VARCHAR(100)", nullable = false)
private String title;

@Column(name = "COMMENT", columnDefinition = "VARCHAR(100)", nullable = false)
private String comment;

@Column(name = "LOCKED", nullable = false)
private boolean locked;

@Column(name = "HIDDEN", nullable = false)
private boolean hidden;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "RULE", joinColumns = {@JoinColumn(name = "PLATE_ID")}, inverseJoinColumns = {@JoinColumn(name = "ACCOUNT_ID")})
private java.util.Collection<Account> masters;


public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public java.lang.String getTitle() {
return title;
}

public void setTitle(java.lang.String title) {
this.title = title;
}

public java.lang.String getComment() {
return comment;
}

public void setComment(java.lang.String comment) {
this.comment = comment;
}

public boolean getLocked() {
return locked;
}

public void setLocked(boolean locked) {
this.locked = locked;
}

public boolean getHidden() {
return hidden;
}

public void setHidden(boolean hidden) {
this.hidden = hidden;
}

public java.util.Collection<Account> getMasters() {
return masters;
}

public void setMasters(java.util.Collection<Account> masters) {
this.masters = masters;
}
}

然后我们企图通过访问plate.getMasters()把版主的Username放入一个selectOneMenu,很遗憾,这是不可能的,因为selectOneMenu接收的是一个
Collection<SelectItem>组件。

怎么解决?我们可以在PlateBacking中创建一个内部类PlateBean


package com.bbpop.jsf.backing;

import com.bbpop.entity.Account;
import com.bbpop.entity.Plate;
import com.bbpop.service.PlateService;
import java.util.Collection;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.model.SelectItem;
/**
*
* @author vlinux
*/

public class PlateBacking {

private HtmlDataTable plateDataTable;

public class PlateBean extends Plate {
private Collection<SelectItem> masters;
public Collection<SelectItem> getMasterUsernames() {return masters;}
public PlateBean( Plate plate ) {
this.setComment(plate.getComment());
this.setHidden(plate.getHidden());
this.setId(plate.getId());
this.setLocked(plate.getLocked());
this.setTitle(plate.getTitle());
masters = new java.util.ArrayList<SelectItem>();
for( Account master : plate.getMasters() ) {
masters.add( new SelectItem(master.getUsername(),master.getUsername()) );
}
}
}

public Collection<PlateBean> getPlates() {
Collection<PlateBean> plates = new java.util.ArrayList<PlateBean>();
for( Plate plate : plateService.findByUnHidden() ) {
plates.add( new PlateBean(plate) );
}
return plates;
}

private PlateService plateService;
public void setPlateService( PlateService plateService ) {
this.plateService = plateService;
}

}

这样我们就可以很轻松的通过JSF的核心标签<f:selectItems/>将原本属于Collection<Account>的东西转换到其中

总结:灵活运用内部类能解决很多问题。如果这个内部类能抽象出来则抽象出来作为JSF的一个独立的托管BEAN更好。

搜索更多相关主题的帖子: JSF Collection 组件 
2007-09-02 16:52
快速回复:JSF : 将Collection集合内容放入selectOneMenu组件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036921 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved