Using POJO's with SelectOneMenu with Generic Converter

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

Hello How to use List of POJO's with JSF h:selectOneMenu or Primefaces p:selectOneMenu?
I know that there are lot of related questions which suggest to use Converter but no clear build from scratch example.

I want a generic converter code for the above Purpose.

Please suggest any alternatives/point me to the right question if its a duplicate.

回答1:

here is a complete example of using a POJO with Primefaces p:selectOneMenu.the primfaces select one menu display a list of students.If you press details button after select any student, a primfaces dialog will appear with the full name of this student.

com.model package:

Student class

package com.model;  import java.io.Serializable;  public class Student implements Serializable{   private static final long serialVersionUID = 1L;  private int Id;   public int getId() {     return Id; } public void setId(int id) {     Id = id; }  private String lastName; private String firstName;   public String getLastName() {     return lastName; } public void setLastName(String lastName) {     this.lastName = lastName; } public String getFirstName() {     return firstName; } public void setFirstName(String firstName) {     this.firstName = firstName; }  public Student() {     super();     }      public Student(String lastName, String firstName,int Id) {     super();     this.lastName = lastName;     this.firstName = firstName;     this.Id = Id;     }} 

The converter

   package com.model;   import java.util.ArrayList;  import java.util.List;   import javax.faces.application.FacesMessage;  import javax.faces.component.UIComponent;  import javax.faces.context.FacesContext;  import javax.faces.convert.Converter;  import javax.faces.convert.ConverterException;  import javax.faces.convert.FacesConverter;    @FacesConverter(forClass = com.model.Student.class,value="student")    public class StudentConverter implements Converter{  public static List studentDB;      static {         studentDB = new ArrayList();         studentDB.add(new Student("William", "Wong", 1));         studentDB.add(new Student("John", "Smith", 2));         studentDB.add(new Student("Mari", "Beckley", 3));         studentDB.add(new Student("Messi", "Leonardo",4));         studentDB.add(new Student("William", "Astrid", 5));         studentDB.add(new Student("William", "Banana", 6));          }      public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {         if (submittedValue.trim().equals("")) {             return null;         } else {             try {                 int number = Integer.parseInt(submittedValue);                  for (Student s : studentDB) {                     if (s.getId() == number) {                         return s;                     }                 }              } catch(NumberFormatException exception) {                 throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));             }         }          return null;     }      public String getAsString(FacesContext facesContext, UIComponent component, Object value) {         if (value == null || value.equals("")) {             return "";         } else {             return String.valueOf(((Student) value).getId());         }     } } 

com.managedbean package

   package com.managedbean;     import java.util.List;    import javax.annotation.PostConstruct;    import javax.faces.bean.ManagedBean;    import javax.faces.bean.ViewScoped;    import com.model.Student;    import com.model.StudentConverter;   @ManagedBean  @ViewScoped     public class StudentMB {     private Student  selectedStudent;     public Student getSelectedStudent() {     return selectedStudent; }   public void setSelectedStudent(Student selectedStudent) {     this.selectedStudent = selectedStudent; }   public List getStudents() {     return students; }   public void setStudents(List students) {     this.students = students; }   private List students;   @PostConstruct public void init(){     students=StudentConverter.studentDB; }   } 

selectMenu.xhtml

     

get the source code

you can download the complete example built using eclipse and deployed to glassfish from here



回答2:

In h:selectOneMenu it's possible, but yours POJO must implements a interface with a identifier atrribute.

Interface:

public inteface IConvertible {     public Integer getId(); } 

Pojo:

public class POJO implements IConvertible {     ....     .... } 

And generic converter:

@SuppressWarnings("unchecked") public class GenericConverter implements Converter {      @Override     public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {         Entidade ret = null;         UIComponent src = arg1;         if (src != null) {             List childs = src.getChildren();             UISelectItems itens = null;             if (childs != null) {                 for (UIComponent ui : childs) {                     if (ui instanceof UISelectItems) {                         itens = (UISelectItems) ui;                         break;                     } else if (ui instanceof UISelectItem) {                         UISelectItem item = (UISelectItem) ui;                         try {                             IConvertible val = (IConvertible) item.getItemValue();                             if (arg2.equals("" + val.getId())) {                                 ret = val;                                 break;                             }                         } catch (Exception e) {                         }                     }                 }             }              if (itens != null) {                 List values = (List) itens.getValue();                 if (values != null) {                     for (Entidade val : values) {                         if (arg2.equals("" + val.getId())) {                             ret = val;                             break;                         }                     }                 }             }         }         return ret;     }      @Override     public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {         String ret = "";         if (arg2 != null && arg2 instanceof Entidade) {             Entidade m = (Entidade) arg2;             if (m != null) {                 Long id = m.getId();                 if (id != null) {                     ret = id.toString();                 }             }         }         return ret;     } } 

JSF page:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!