h:selectOneMenu中的f:ajax监听器方法未执行

时间:2011-05-22 18:35:19

标签: ajax jsf jsf-2

使用托管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");
        }
    }
}

更新

我发现了一些有趣的事情:

<f:ajax>代码无效<h:link><h:selectOneMenu><h:button><h:commandButton>。在这种情况下,render属性中的值不正确,但event属性值不正确会产生错误。

<h:outputLabel><h:inputText>正确使用<f:ajax>

2 个答案:

答案 0 :(得分: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>

请注意,在一个有点像Web浏览器的网络浏览器中,如Firefox的Web Developer Toolbar和/或Firebug,当ajax请求到达时,你应该立即注意到像jsf is undefined这样的JS错误被执行。至少应该考虑一些事情。


更新:根据您的更新

  

我发现了一些有趣的事情:

     

<f:ajax>代码无效<h:link><h:selectOneMenu><h:button><h:commandButton>。在这种情况下,render属性中的值不正确,但event属性值不正确会产生错误。

     

<h:outputLabel><h:inputText>正确使用<f:ajax>

<h:link><h:button>仅用于GET请求,而不是POST请求。但是,它应该可以在<h:selectOneMenu><h:commandButton>上正常工作。为简单起见,您是否在问题中省略了完整图片中的代码?你使用哪个JSF impl /版本?你在classpath中使用正确的库吗?看起来你必须搞砸了。

为了说服你(和我)我刚刚创建了以下copy'n'paste'n'runnable testcase

<!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'

答案 1 :(得分:0)

如果你有f:metadataf:viewparam标签,请小心,因为每个ajax请求都会调用参数的设置者。

如果在调用ajax调用时出现任何错误/异常,是否可以提供错误日志?