使用托管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" />
Run Code Online (Sandbox Code Playgroud)
托管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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
我发现了一些有趣的东西:
<f:ajax>标签不工作<h:link>,<h:selectOneMenu>,<h:button>,<h:commandButton>.在这种情况下,render不会注意到event属性中的错误值,但属性的错误值会生成错误.
<h:outputLabel>,<h:inputText>工作与<f:ajax>正常
Bal*_*usC 35
将<f:ajax>需要jsf.js的文件被包含在HTML <head>.它包含了用于执行JSF ajax魔术的所有JS函数.
要实现此目的,请确保使用<h:head>而不是<head>在主模板中.然后,JSF将自动包含<script>指向的必要元素jsf.js.
<!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>
Run Code Online (Sandbox Code Playgroud)
请注意,在一个有点像Web开发者工具集(如Firefox的Web开发人员工具栏和/或Firebug)的网络浏览器中,您应该立即注意到JS错误,例如jsf is undefined何时执行ajax请求.至少应该考虑一些事情.
更新:根据您的更新
我发现了一些有趣的东西:
<f:ajax>标签不工作<h:link>,<h:selectOneMenu>,<h:button>,<h:commandButton>.在这种情况下,render不会注意到event属性中的错误值,但属性的错误值会生成错误.
<h:outputLabel>,<h:inputText>一起工作<f:ajax>正常.
该<h:link>和<h:button>被intented的GET请求,不是POST请求.然而,它应该只是罚款<h:selectOneMenu>和<h:commandButton>.为简单起见,您是否在问题中省略了完整图片中的代码?你使用哪个JSF impl /版本?你在classpath中使用正确的库吗?看起来你必须搞砸了.
为了说服你(和我)我刚刚创建了以下copy'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>
Run Code Online (Sandbox Code Playgroud)
用这个豆子
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在Tomcat 7.0.12上,它可以与Mojarra 2.1.1一起运行.
INFO: Starting Servlet Engine: Apache Tomcat/7.0.12
INFO: Initializing Mojarra 2.1.1 (FCS 20110408) for context '/playground'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75591 次 |
| 最近记录: |