h:selectOneMenu 中的 f:ajax 监听方法没有执行
该页面使用托管 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");
}
}
}
更新:
我发现了一些有趣的事情:
标记在
、
、
、
。在这种情况下,不会注意到 render
属性中的错误值,但 event
属性的错误值会生成错误。
、
与
正确配合
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.
<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 render
attribute is not noticed, but incorrect value of event
attribute generate an error.
<h:outputLabel>
, <h:inputText>
work with <f:ajax>
properly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要求将jsf.js
文件包含在 HTML中。它包含用于执行 JSF ajax 魔法的所有 JS 函数。
为此,请确保您在主模板中使用
而不是。然后,JSF 将自动包含指向
jsf.js
的必要元素。
请注意,在一个像 Firefox 的 Web 开发人员工具栏 和/或者 Firebug 当 ajax 请求被执行时,你应该立即注意到 JS 错误,例如
jsf is undefined
被执行。这至少应该给人一些思考的机会。更新:根据您的更新
和
仅适用于 GET 请求,不适用于 POST 请求。不过,它在
和
上应该可以正常工作。为了简单起见,您是否在完整的图片中添加了更多代码,而您在问题中省略了这些代码?您使用的是哪个 JSF impl/版本?您在类路径中使用了正确的库吗?看来你确实搞砸了一些事情。创建了以下复制'n'粘贴'n'可运行测试用例,
为了说服您(和我自己),我刚刚使用此 bean
它在 Tomcat 7.0.12 上与 Mojarra 2.1.1 一起运行良好。
The
<f:ajax>
requiresjsf.js
file being included in the HTML<head>
. It contains all JS functions for doing the JSF ajax magic.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 tojsf.js
.Note that in a bit decent webbrowser with a bit decent webdeveloper toolset like Firefox's Web Developer Toolbar and/or Firebug you should immediately have noticed JS errors like
jsf is undefined
when the ajax request is to be executed. That should at least have given something to think about.Update: as per your update
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.To convince you (and myself) I just created the following copy'n'paste'n'runnable testcase
with this bean
It runs fine with Mojarra 2.1.1 on Tomcat 7.0.12.
如果您有
f:metadata
和f:viewparam
标记,请小心,因为每个 ajax 请求都会调用参数的设置器。如果在调用ajax调用时生成任何错误/异常,您能否提供错误日志?
Be careful if you have
f:metadata
andf:viewparam
tags since the setters of the parameters will be called with every ajax request.Can you provide the error log if there is any error/exception that is being generated when you call the ajax call?