2012-09-10 50 views
-2

當我按下此按鈕時,test()方法不能打印任何值。並且System.out.println(person);總是在handleCityChange()中打印爲空,並且幀顯示爲紅色,並且右上角跳出值爲無效消息。我該如何解決它?謝謝大家。primefaces <p:select>無法獲取值

的index.xhtml

<h:body> 
    <h:form> 
    <p:fieldset legend="Modify" toggleable="true" toggleSpeed="200" collapsed="true"> 
     <h:panelGrid columns="2" cellpadding="10" id="modify_change"> 
      <h:outputLabel value="Department :"/> 
      <p:selectOneMenu id="modify" value="#{modify.department}" style="width: 150px"> 
      <f:selectItem itemLabel="Choose Department" itemValue=""/> 
      <f:selectItems value="#{modify.departments}" /> 
      <p:ajax update="modify_delete" listener="#{modify.handleCityChange()}" /> 
      </p:selectOneMenu> 
     <h:outputLabel value="Choose Employee" /> 
      <p:selectOneMenu id="modify_delete" value="#{modify.person}" style="width: 150px"> 
      <f:selectItem itemLabel="Choose Employee" itemValue=""/> 
      <f:selectItems value="#{modify.persons}" /> 
      </p:selectOneMenu> 
     </h:panelGrid> 
     <h:commandButton value="Go to Modify !" actionListener="#{modify.test()}"/> 
    </p:fieldset> 
    </h:form> 
</h:body> 

Java代碼的

@ManagedBean 
@SessionScoped 
public class modify { 

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.mycompany_SuneCoolingSystem_war_1.0-SNAPSHOTPU"); 
    EmployeeJpaController jpaController = new EmployeeJpaController(null, emf); 
    EntityManager e = jpaController.getEntityManager(); 
    private Map<String, String> departments = new HashMap<String, String>(); 
    private Map<String, String> persons = new HashMap<String, String>(); 
    private Map<String, Map<String, String>> allocatoin = new HashMap<String, Map<String, String>>(); 
    private String department; 
    private String person; 

    public modify() { 
    Query q = e.createNamedQuery("Employee.findAll"); 
    List resultList = q.getResultList(); 
    for (int i = 0; i < resultList.size(); i++) { 
     Employee result = (Employee) resultList.get(i); 
     departments.put(result.getDepartment(), result.getDepartment()); 
    } 
    q = e.createNamedQuery("Employee.findByDepartment"); 
    q.setParameter("department", department); 
    resultList = q.getResultList(); 
    } 

    public void handleCityChange() { 
    if (department != null && !department.equals("")) { 
     Query q = e.createNamedQuery("Employee.findByDepartment"); 
     q.setParameter("department", department); 
     List resultList = q.getResultList(); 
     persons.clear(); 
     for (int j = 0; j < resultList.size(); j++) { 
      Employee result = (Employee) resultList.get(j); 
      persons.put(result.getName(), result.getName()); 
     } 
    } else { 
     persons = new HashMap<String, String>(); 
    } 
    System.out.println(departments); 
    System.out.println(department); 
    System.out.println(persons); 
    System.out.println(person); 
    } 
    public void test() { 
    System.out.println(departments); 
    System.out.println(department); 
    System.out.println(persons); 
    System.out.println(person); 
    } 

     //getter() and setter() 
} 

回答

0

你做錯了幾件事情:

  1. person永遠不會被設置!。因此它始終爲空,當調用getPerson()時,它將返回null,就像那樣簡單。我想你應該使用ViewScoped bean。有關更多說明,請參閱this question
  2. 您正在使用Map<String, String>,您應該使用List<String>或任何其他CollectionMap s用於鍵值對。
  3. 爲什麼要查詢一組Employee s,然後遍歷它以得到它們的Department?你爲什麼不查詢Department s的列表?像select e.department from Employee e
  4. 構造函數的最後一部分沒有效果。 Queryq不會再次使用,因爲resultlistdepartment屬性爲空。

    q = e.createNamedQuery("Employee.findByDepartment"); 
    q.setParameter("department", department); 
    resultList = q.getResultList(); 
    
  5. 您正在使用持久性/數據庫的東西在後臺bean。這通常是非常糟糕的做法。

0

變化(ActionListener的>動作):

<h:commandButton value="Go to Modify !" actionListener="#{modify.test()}"/> 

要:

<h:commandButton value="Go to Modify !" action="#{modify.test()}"/> 

,並把所有到<h:form></h:form>

問候,

+0

我嘗試2種方法。但測試方法不工作。並且框架顯示紅色並跳出右上角的值是無效消息, – user1630854

+0

表示您試圖在兩次選擇中立即更新?