2013-09-27 21 views
4

你好如何使用JSF的POJO列表h:selectOneMenu或Primefaces p:selectOneMenu
我知道,有很多相關的問題,建議使用轉換器,但沒有從頭開始清晰的構建示例。使用POJO與SelectOneMenu一起使用通用轉換器

我想要一個用於上述目的的通用轉換器代碼。

請建議任何替代品/指向我的正確的問題,如果它的重複。

+0

http://stackoverflow.com/questions/3621251/how-to-write-a-custom-converter-when-working-with-primefaces-components-that-c​​o – rags

回答

5

這裏是使用POJO和Primefaces p:selectOneMenu.the primfaces選擇一個菜單顯示學生列表的完整示例。如果在選擇任何學生後按詳細信息按鈕,則會出現一個primfaces對話框,其全名這個學生。

com.model包:

Student類

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; 


    }} 

轉換器

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<Student> studentDB; 

    static { 
     studentDB = new ArrayList<Student>(); 
     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 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<Student> getStudents() { 
    return students; 
} 


public void setStudents(List<Student> students) { 
    this.students = students; 
} 


private List<Student> students; 


@PostConstruct 
public void init(){ 
    students=StudentConverter.studentDB; 
} 

} 

selectMenu.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 
    <h:head> 
</h:head> 
<h:body> 
<h:form> 
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="1"> 
<h:outputText value="Pojo: " /> 
    <p:selectOneMenu value="#{studentMB.selectedStudent}" effect="fade" converter="student"> 
     <f:selectItem itemLabel="Select One" itemValue="" /> 
     <f:selectItems value="#{studentMB.students}" var="student" itemLabel="#{student.firstName}" itemValue="#{student}"/> 
    </p:selectOneMenu> 
    </h:panelGrid> 
    <p:commandButton value="Details" update="display" oncomplete="dlg.show()" /> 

<p:dialog header="Selected Value" modal="true" showEffect="fade" hideEffect="fade" widgetVar="dlg"> 
<h:panelGrid columns="1" id="display"> 
    <h:outputText value="#{studentMB.selectedStudent.firstName} #{studentMB.selectedStudent.lastName}" rendered="#{not empty studentMB.selectedStudent}" /> 
    </h:panelGrid> 
    </p:dialog> 
    </h:form> 


</h:body> 
</html> 

獲得源代碼

您可以下載使用eclipse構建和部署從here

+0

在我的情況是必要的爲學生添加一個equals方法(當然還有一個hashCode()) –

3

在h至Glassfish的完整的例子:selectOneMenu用於這是可能的,但你的POJO必須實現一個接口帶有標識符atrribute。

接口:

public inteface IConvertible { 
    public Integer getId(); 
} 

POJO的:

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

和通用變頻器:

@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<UIComponent> 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<IConvertible> values = (List<IConvertible>) 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頁面:

<h:selectOneMenu id="combo" value="#{pojoMB.pojo}" converter="genericConverter"> 
    <f:selectItem itemLabel="-- Select one --" itemValue="" /> 
    <f:selectItems value="#{pojoMB.pojoList}" var="pj" itemValue="#{pj}" itemLabel="#{pj.labelOfPojo}" /> 

相關問題