2015-08-19 16 views
0

我試圖從selectOneMenu中獲得一個包含名字,姓氏等字段的對象。這是我的表格:@FacesConverter類中的@Inject PersonDao爲空

<h:form> 

<p:outputLabel value="Persons: " /> 
<p:selectOneMenu value="#{personBean.person}"> 
    <f:selectItem itemLabel="Select Person" itemValue="" noSelectionOption="true"/> 
    <f:selectItems itemLabel="#{person.firstName}" itemValue="#{person}" var="person" value="#{personBean.persons}" /> 

</p:selectOneMenu> 
<br /><br /> 
<p:commandButton value="Submit" 
    action="#{personBean.showSomething()}" icon="ui-icon-check" /> 


我不明白..我在哪裏去了?我怎樣才能得到這個對象?
我一直在嘗試幾天,但我還沒有設法解決這個問題... 我試過使用Converter,但我不斷得到NPE。

編輯
這是我的轉換器:(我想從我的Postgres獲取對象數據庫與我的DAO的幫助下)

@Named 
@FacesConverter(forClass=Person.class) 
public class PersonConverter implements Converter { 

    @Inject 
    private PersonDao personDao; 

    @Override 
    public Object getAsObject(FacesContext arg0, UIComponent component, String submittedValue) { 
     if (submittedValue == null || submittedValue.isEmpty()) { 
      return null; 
     } 

     try { 
      Person p = personDao.findById(Long.valueOf(submittedValue)); 
      return p; 
     } catch (NumberFormatException e) { 
      throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Person ID"), e); 
     } 
    } 

    @Override 
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) { 
     if (arg2 == null) { 
      return ""; 
     } 

     if (arg2 instanceof Person) { 
      return String.valueOf(((Person) arg2).getId()); 
     } else { 
      throw new ConverterException(new FacesMessage(arg2 + " is not a valid Person")); 
     } 
    } 

} 

這是我得到一個NPE Person p = personDao.findById(Long.valueOf(submittedValue));
find()方法在其他地方工作... 當我使用調試器時,我注意到personDao爲空。我怎樣才能解決這個問題?

+0

只有當我使用該轉換器。如果沒有,'personBean.showInfo()'甚至沒有被調用,所以它沒有返回任何東西。 – ffs

+0

請添加更多信息。就像你的堆棧跟蹤你的NPE。另外,不應該itemValue是#{人}?而不是#{person.firstName}? – Fritz

+0

謝謝指出! – ffs

回答

0
  • 在頁面:

     <p:selectOneMenu value="#{personBean.person}"> 
          <f:selectItem itemLabel="Select Person" itemValue="" noSelectionOption="true"/> 
          <f:selectItems itemLabel="#{person.firstName}" itemValue="#{person}" var="person" value="#{personBean.persons}" /> 
          <f:converter binding="#{personConverter}" /> 
          <f:attribute name="attrpersons" value="#{personBean.persons}" />      
         </p:selectOneMenu> 
    
  • 在人豆:

    您必須實現的toString和equals方法。

  • 最後,你必須添加轉換器類:

    @Named 
    public class PersonConverter implements Converter{ 
    
        public PersonConverter(){} 
    
        @Override 
         public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) { 
          List<Person> persons = (List<Person>)component.getAttributes().get("attrpersons"); 
         if (submittedValue.trim().equals("")) { 
          return null; 
         } else { 
          Iterator<Person> it = persons.iterator(); 
          while(it.hasNext()){ 
           Person p = it.next(); 
           if(p.toString().equals(submittedValue)) 
            return p;     
          } 
         } 
         return null; 
        } 
    
        @Override 
        public String getAsString(FacesContext facesContext, UIComponent component, Object value) { 
         if (value == null) { 
          return ""; 
         } else { 
          return value.toString(); 
         } 
        } 
    }