按下此按钮时,test()
方法无法打印任何值。并且System.out.println(person);
始终在handleCityChange()
中打印空,并且框架显示为红色,右上角跳出值为无效消息。我该如何解决?谢谢大家。
的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 :(得分:0)
更改(actionListener&gt;操作):
<h:commandButton value="Go to Modify !" actionListener="#{modify.test()}"/>
要:
<h:commandButton value="Go to Modify !" action="#{modify.test()}"/>
全部放入<h:form></h:form>
此致
答案 1 :(得分:0)
你做错了几件事:
person
永远不会被设置!。因此它始终为null,当调用getPerson()
时,它将返回null,就像那样简单。SessionScoped
bean,我认为您应该使用ViewScoped
bean。有关详细说明,请参阅this question。Map<String, String>
,您应该使用List<String>
或任何其他Collection
。 Map
用于键值对。Employee
然后循环通过它来获取Department
?为什么不查询Department
的列表?像select e.department from Employee e
?构造函数的最后一部分无效。 Query
q
永远不再使用,resultlist
和department
属性为空。
q = e.createNamedQuery("Employee.findByDepartment");
q.setParameter("department", department);
resultList = q.getResultList();
您在支持bean中使用持久性/数据库内容。这通常是非常糟糕的做法。