gpt4 book ai didi

ajax - f :ajax listener method in h:selectOneMenu is not executed

转载 作者:行者123 更新时间:2023-12-04 00:04:53 24 4
gpt4 key购买 nike

该页面使用托管 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 的值不正确属性产生错误。
<h:outputLabel> , <h:inputText><f:ajax> 一起工作适本地

最佳答案

<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>
请注意,在有点像 Firefox 的 Web Developer Toolbar 的有点像样的 web 开发者工具集的有点像样的网络浏览器中。和/或 Firebug你应该立即注意到 JS 错误,如 jsf is undefined当 ajax 请求被执行时。这至少应该让我们思考一些事情。

更新 : 根据你的更新

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.

<h:link><h:button>仅用于 GET 请求,而不是 POST 请求。但是它应该可以在 <h:selectOneMenu> 上正常工作和 <h:commandButton> .为简单起见,您在问题中省略了更多代码吗?您使用的是哪个 JSF 实现/版本?您是否在类路径中使用了正确的库?看起来你一定是真的搞砸了什么。
为了说服你(和我自己),我刚刚创建了以下 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>
用这个 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;
}

}
它与 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'

关于ajax - f :ajax listener method in h:selectOneMenu is not executed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com