在托管bean中使用适当的值正确生成了页面,但是这两个h:selectOneMenus中的ajax事件不起作用。不调用侦听器。标签内一定有错误,但我看不到。
<f:view> <h:form> <h:messages /> <h:panelGrid columns="3"> <h:outputLabel value="Choose your faculty: *" for="faculties" /> <h:selectOneMenu id="faculties" value="#{registrateStudent.selectedFaculty}" > <f:ajax event="change" listener="#{registrateStudent.genSpecializations}" execute="faculties" render="specializations" /> <f:selectItems value="#{registrateStudent.listFaculty}" var="curFac" itemLabel="#{curFac.name}" itemValue="#{curFac}" /> </h:selectOneMenu> <h:message id="message_faculties" for="faculties" /> <h:outputLabel value="Choose your specialization: *" for="specializations" /> <h:selectOneMenu id="specializations" value="#{registrateStudent.selectedSpecialization}" > <f:selectItems value="#{registrateStudent.listSpecialization}" var="curSpec" itemLabel="#{curSpec.name}" itemValue="#{curSpec}"/> </h:selectOneMenu> <h:message id="message_specializations" for="specializations" />
托管Bean:
@ManagedBean(name = "registrateStudent") @ViewScoped public class RegistrateStudent { private Faculty selectedFaculty; private List<Faculty> listFaculty; private Specialization selectedSpecialization; private List<Specialization> listSpecialization; private boolean showSpecialization = false; /** Creates a new instance of RegistrateStudent */ public RegistrateStudent() { users = new Users(); System.out.println("poaposd1"); student = new Student(); } @PostConstruct public void init() { listFaculty = ff.findAll(); if (listFaculty != null) { selectedFaculty = listFaculty.get(0); listSpecialization = sf.findByFaculty(selectedFaculty.getIdFaculty()); if (listSpecialization != null) { selectedSpecialization = listSpecialization.get(0); } else {} } else {} } public void genSpecializations(AjaxBehaviorEvent event) { if (sf.findByFaculty(selectedFaculty.getIdFaculty()) != null) { this.showSpecialization = true; } else { JsfUtil.addSuccessMessage("faculties", "We don't have specializations for such faculty"); } } }
更新:
我发现了一些有趣的东西:
<f:ajax>标签不工作<h:link>,<h:selectOneMenu>,<h:button>,<h:commandButton>。在这种情况下,render不会注意到event属性中的错误值,但是属性的错误值会产生错误。
<f:ajax>
<h:link>
<h:selectOneMenu>
<h:button>
<h:commandButton>
render
event
<h:outputLabel>,<h:inputText>可以<f:ajax>正常使用
<h:outputLabel>
<h:inputText>
该<f:ajax>要求jsf.js包含在HTML文件之中<head>。它包含用于执行JSF ajax魔术的所有JS函数。
jsf.js
<head>
为此,请确保在主模板中使用<h:head>而不是<head>。然后,JSF将自动在其中包含<script>指向的必要元素jsf.js。
<h:head>
<script>
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <title>Look, with h:head</title> </h:head> <h:body> Put your content here. </h:body> </html>
请注意,在带有Firefox的Web Developer Toolbar和/或Firebug的Web开发人员工具集的Web浏览器中,您应该立即注意到JS错误,例如jsf is undefined将执行ajax请求时。至少应该考虑一下。
jsf is undefined
更新 :根据您的更新
我发现了一些有趣的东西: <f:ajax>标签不工作<h:link>,<h:selectOneMenu>,<h:button>,<h:commandButton>。在这种情况下,render不会注意到event属性中的错误值,但是属性的错误值会产生错误。 <h:outputLabel>,<h:inputText>可以<f:ajax>正常使用。
<h:outputLabel>,<h:inputText>可以<f:ajax>正常使用。
该<h:link>和<h:button>被intented的GET请求,不是POST请求。但是,它应该在<h:selectOneMenu>和上都可以正常工作<h:commandButton>。为了简化起见,您没有在问题中省略完整代码的更多代码吗?您正在使用哪个JSF展示/版本?您是否在classpath中使用了正确的库?看来您确实必须弄乱了某些东西。
为了说服您(和我自己),我刚刚创建了以下副本’n’paste’n’runnable测试用例
<!DOCTYPE html> <html lang="en" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" > <h:head> <title>SO question 6089924</title> </h:head> <h:body> <h:form> <h:selectOneMenu value="#{bean.selected}"> <f:selectItem itemValue="#{null}" itemLabel="Select..." /> <f:selectItem itemValue="one" /> <f:selectItem itemValue="two" /> <f:selectItem itemValue="three" /> <f:ajax listener="#{bean.listener}" render="result" /> </h:selectOneMenu> <h:commandButton value="commandButton" action="#{bean.submit}"> <f:ajax listener="#{bean.listener}" render="result" /> </h:commandButton> <h:outputText id="result" value="#{bean.selected} #{bean.result}" /> <h:messages /> </h:form> </h:body> </html>
用这个豆
package com.example; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.event.AjaxBehaviorEvent; @ManagedBean @ViewScoped public class Bean implements Serializable { private String selected; private String result; public void submit() { System.out.println("submit"); } public void listener(AjaxBehaviorEvent event) { System.out.println("listener"); result = "called by " + event.getComponent().getClass().getName(); } public String getSelected() { return selected; } public void setSelected(String selected) { this.selected = selected; } public String getResult() { return result; } }
它与Mojarra 2.1.1在Tomcat 7.0.12上运行良好。
INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 INFO: Initializing Mojarra 2.1.1 (FCS 20110408) for context '/playground'