h:selectOneMenu 中的 f:ajax 监听器方法没有执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6089924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The f:ajax listener method in h:selectOneMenu is not executed
提问by kolobok
The page is generated correctly with appropriate values in managed bean, but ajax events in these two h:selectOneMenus don't works. Listener is not called. An error has to be somewhere within tags, but I don't see it.
该页面使用托管 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" />
Managed 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");
}
}
}
UPDATE:
更新:
I've found out a few interesting things:
我发现了一些有趣的事情:
<f:ajax>tag doesn't work at <h:link>, <h:selectOneMenu>, <h:button>, <h:commandButton>. In this cases incorrect values in renderattribute is not noticed, but incorrect value of eventattribute generate an error.
<f:ajax>标记在<h:link>, <h:selectOneMenu>, <h:button>,处不起作用<h:commandButton>。在这种情况下,render不会注意到属性中的不正确值,但不正确的event属性值会产生错误。
<h:outputLabel>, <h:inputText>work with <f:ajax>properly
<h:outputLabel>,<h:inputText>与工作<f:ajax>正常
回答by BalusC
The <f:ajax>requires jsf.jsfile being included in the HTML <head>. It contains all JS functions for doing the JSF ajax magic.
包含在 HTML 中的<f:ajax>requiresjsf.js文件<head>。它包含用于执行 JSF ajax 魔术的所有 JS 函数。
To achieve this, ensure that you're using <h:head>instead of <head>in the master template. JSF will then automatically include the necessary <script>element there pointing to jsf.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>
Note that in a bit decent webbrowser with a bit decent webdeveloper toolset like Firefox's Web Developer Toolbarand/or Firebugyou should immediately have noticed JS errors like jsf is undefinedwhen the ajax request is to be executed. That should at least have given something to think about.
请注意,在一个像 Firefox 的Web 开发人员工具栏和/或Firebug这样的像样的Web浏览器和像样的Web 开发人员工具集一样,您应该立即注意到 JS 错误,例如jsf is undefined在执行 ajax 请求时。这至少应该让我们思考一些事情。
Update: as per your update
更新:根据您的更新
I've found out a few interesting things:
<f:ajax>tag doesn't work at<h:link>,<h:selectOneMenu>,<h:button>,<h:commandButton>. In this cases incorrect values inrenderattribute is not noticed, but incorrect value ofeventattribute generate an error.
<h:outputLabel>,<h:inputText>work with<f:ajax>properly.
我发现了一些有趣的事情:
<f:ajax>标记在<h:link>,<h:selectOneMenu>,<h:button>,处不起作用<h:commandButton>。在这种情况下,render不会注意到属性中的不正确值,但不正确的event属性值会产生错误。
<h:outputLabel>,<h:inputText>工作<f:ajax>得当。
The <h:link>and <h:button>are intented for GET requests only, not POST requests. It should however work just fine on <h:selectOneMenu>and <h:commandButton>. Don't you have more code into the complete picture which you omitted from the question for simplicity? Which JSF impl/version are you using? Are you using the right libraries in classpath? It look like that you must really have messed up something.
该<h:link>和<h:button>被intented的GET请求,不是POST请求。但是,它应该可以在<h:selectOneMenu>和上正常工作<h:commandButton>。为简单起见,您在问题中省略了更多代码吗?您使用的是哪个 JSF 实现/版本?您是否在类路径中使用了正确的库?看起来你一定是真的搞砸了什么。
To convince you (and myself) I just created the following copy'n'paste'n'runnable testcase
为了说服你(和我自己),我刚刚创建了以下 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>
with this bean
用这个豆
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;
}
}
It runs fine with Mojarra 2.1.1 on Tomcat 7.0.12.
它与 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'
回答by aseychell
Be careful if you have f:metadataand f:viewparamtags since the setters of the parameters will be called with every ajax request.
如果你有f:metadataandf:viewparam标签,请小心,因为每个 ajax 请求都会调用参数的设置器。
Can you provide the error log if there is any error/exception that is being generated when you call the ajax call?
如果在调用 ajax 调用时产生任何错误/异常,您能否提供错误日志?

