问题描述
您好如何在JSF h:selectOneMenu
或Primefaces p:selectOneMenu
中使用POJO列表?
我知道有很多相关问题建议使用Converter,但没有从头开始构建的清晰示例.
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.
请提出其他选择,如果有重复,请向我指出正确的问题.
Please suggest any alternatives/point me to the right question if its a duplicate.
推荐答案
下面是将POJO与Primefaces p:selectOneMenu一起使用的完整示例.primfaces选择一个菜单会显示学生列表.任何学生,都会出现带有该学生全名的primfaces对话框.
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软件包:
com.model package:
学生班
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软件包
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<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
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>
获取源代码
您可以从此处
这篇关于通过泛型转换器将POJO与SelectOneMenu一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!