我從primefaces使用selectOneMenu填充來自mysql數據庫的數據,但我經常得到空字段,我沒有收到任何錯誤。 有人可以解釋我做錯了什麼?使用jsf2從<p:selectOneMenu>中的數據庫填充數據
這是我的代碼:
XHTML文件:
<p:selectOneMenu id="point" value="#{pointController.selectedPoint}"
var="items_var">
<f:selectItems value="#{pointController.points}" var="points_var"
itemLabel="#{points_var.name}" itemValue="#{points_var.name}"/>
</p:selectOneMenu>
管理的bean:
@ManagedBean(name = "pointController")
@SessionScoped
public class PointController implements Serializable {
@EJB
private ethoam.entity.PointFacade ejbFacade;
private List<point> points;
private point selectedPoint;
private point[] selectedPoints;
private List<Point> selectedPointsList;
private SelectItem[] PointNamesOptions;
public PointController() {
this.initList();
}
@PostConstruct
private void initList() {
points = new ArrayList<point>(pointControllerConverter.getPoints().values());
}
// getters setters
}
器和轉換器類:
public static class PointControllerConverter implements Converter {
@PersistenceContext(unitName = "PointPU")
private static EntityManager ejbfacade;
public static Map<String, point> points = new HashMap<String, point>();
public Map<String,point> getPoints() {
points = (Map<String, point>) ejbfacade.getEntityManager().createNamedQuery("SELECT d FROM point d WHERE d.name = :name");
return points;
}
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
PointController controller = (PointController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "pointController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof point) {
point o = (point) object;
return getStringKey(o.getIdPoint());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Point.class.getName());
}
}
}
我試過了,但沒有成功。 – user506246
您是如何註冊PointControllerConverter的? –
@FacesConverter(forClass = Point.class) – user506246