【问题标题】:primefaces <p:select> can't get valuesprimefaces <p:select> 无法获取值
【发布时间】:2012-09-10 15:46:29
【问题描述】:

当我按下此按钮时,test() 方法无法打印任何值。而System.out.println(person);总是在handleCityChange()中打印null,并且边框显示为红色,右上角跳出value is invalid信息。我该如何解决?谢谢大家。

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()
}

【问题讨论】:

    标签: jsf jpa primefaces


    【解决方案1】:

    你做错了几件事:

    1. person 永远不会被设置!。因此它总是null,当getPerson()被调用时,它会返回null,就这么简单。
    2. 您正在使用SessionScoped bean,我认为您应该使用ViewScoped bean。更多解释请参见this question
    3. 您正在使用Map&lt;String, String&gt;,而您应该使用List&lt;String&gt; 或任何其他CollectionMaps 用于键值对。
    4. 为什么要查询一组Employees,然后通过它循环得到他们的Department?为什么不查询Departments 的列表? select e.department from Employee e 之类的东西?
    5. 构造函数的最后一部分无效。 Query q 不再使用,resultlist 和属性 department 为空。

      q = e.createNamedQuery("Employee.findByDepartment");
      q.setParameter("department", department);
      resultList = q.getResultList();
      
    6. 您在支持 bean 中使用了持久性/数据库内容。这通常是非常糟糕的做法。

    【讨论】:

      【解决方案2】:

      改变(actionListener > action):

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

      收件人:

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

      并将所有内容放入&lt;h:form&gt;&lt;/h:form&gt;

      问候,

      【讨论】:

      • 我为 。但是测试方法不起作用。并且frame显示红色,右上角跳出value is invalid message,
      • 表示你尝试在两个选择中立即更新?
      猜你喜欢
      • 1970-01-01
      • 2014-02-25
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2020-06-04
      相关资源
      最近更新 更多