-2

When I push this button, the test() method can't print any value. And the System.out.println(person); always print null in handleCityChange(), and frame displays red and the upper right corner to jump out value is invalid message. How can I resolve it ? Thanks All.

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 Code

@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()
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

0

Change (actionListener > action):

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

To:

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

And put all into <h:form></h:form>

Regards,

Manu Navarro
  • 700
  • 6
  • 9
0

You're doing several things wrong:

  1. person never gets set!. Hence it is always null, when getPerson() is called it will return null, simple as that.
  2. You're using a SessionScoped bean where I think you should be using a ViewScoped bean. See this question for more explanation.
  3. You're using Map<String, String> where you should be using a List<String> or any other Collection. Maps are for key-value pairs.
  4. Why query for a set of Employees and then loop through it to get their Department? Why don't you query for a list of Departments? Something like select e.department from Employee e?
  5. The last part of the constructor has no effect. The Query q is never used again, as is the resultlist and the property department is null.

    q = e.createNamedQuery("Employee.findByDepartment");
    q.setParameter("department", department);
    resultList = q.getResultList();
    
  6. You're using persistence/database stuff in backing beans. This is generally very bad practice.

Community
  • 1
  • 1
siebz0r
  • 18,867
  • 14
  • 64
  • 107