如何返回getAsObject值

时间:2012-08-13 06:52:18

标签: jsf-2

请问如何在转换器中从此方法返回性别。

我有一个性别实体:

@Entity
public class Gender implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String gender;

使用selectItem来检索所有性别的managedBean,如

public List<SelectItem> getGenderNoList() {
    List<Gender> genderList;
    Gender gender;
    List<SelectItem> selectedGender = new ArrayList<SelectItem>();
    genderList = appSessionBean.getAllGender();
    for(Gender g : genderList) {
        SelectItem s = new SelectItem(g.getGender());
        selectedGender.add(s);
    }
    return selectedGender;
}

和转换器

public Object getAsObject(FacesContext context, UIComponent component, String value) {


}

1 个答案:

答案 0 :(得分:0)

一种方法可以将性别对象:id和性别放在SelectItem的值

 SelectItem s = new SelectItem(g.getGender()+"UniqeSeperator"+g.getId(),g.getGender());

然后在getAsObject(做类似的事情

...
String[] tmpArr = value.split("UniqeSeperator");
return new Gender(Long.valueOf(tmpArr[0]),tmpArr[1]);
像这样......