2013-12-18 96 views
0

我從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()); 
     } 
    } 

} 

回答

1

您正在將字符串定義爲值(itemValue="#{points_var.name}"),並且您的組件期望的實例。

這是你需要考慮的一個變化:

<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}"/> 
</p:selectOneMenu> 

我也注意到,您使用的不是PointFacade填充private List<Point> points;。相反,您正在使用來自PointControllerConverter內的空靜態Map的值實例化ArrayList。

你的問題表明你正試圖用數據庫(MySQL)的值填充selectOnMenu。在這種情況下,您需要重新考慮策略並開始使用某些EJB(如PointFacade)從數據庫中獲取數據。

+0

我試過了,但沒有成功。 – user506246

+0

您是如何註冊PointControllerConverter的? –

+0

@FacesConverter(forClass = Point.class) – user506246

0

檢查一次而不使用屬性 - var,itemValue,itemLabel,您也可以嘗試在getter本身中編寫查詢。