2011-05-22 58 views
10

使用託管bean中的適當值正確生成頁面,但這兩個h:selectOneMenus中的ajax事件不起作用。監聽器沒有被調用。錯誤必須在標籤內的某處,但我沒有看到。h:selectOneMenu中的f: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"); 
     } 
    } 
} 

UPDATE:

我發現了一些有趣的東西:

<f:ajax>標籤沒有在<h:link>工作,<h:selectOneMenu><h:button>,<h:commandButton>。在這種情況下,render屬性中的錯誤值不會被注意到,但錯誤值event屬性會生成錯誤。

<h:outputLabel><h:inputText><f:ajax>正常工作

+0

你嘗試執行=「@ this」而不是execute =「faculties」? – 2011-05-22 20:17:10

+0

@George是的,它試圖 – kolobok 2011-05-24 22:21:57

回答

32

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

注意,在帶着幾分像樣webdeveloper工具集好像有點像樣的網頁瀏覽器Firefox的Web Developer Toolbar和/或Firebug你應該立即注意到像jsf is undefined JS錯誤時,Ajax請求將被執行。至少應該考慮一些事情。


更新:根據您的更新

我發現了一些有趣的東西:

<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 /版本?你在類路徑中使用正確的庫嗎?它看起來像你一定真的搞砸了一些東西。

說服你(和我),我只是創建了以下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; 
    } 

} 

它運行罰款鑽嘴魚科2.1。 1在Tomcat 7.0.12上。

INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 
INFO: Initializing Mojarra 2.1.1 (FCS 20110408) for context '/playground' 
+0

。由於POST請求,當我在''下移動''標記時,我想'value =「#{bean.selected}」'沒有映射到bean的屬性?爲什麼h:outputLabel在沒有h:form的情況下不工作,即使這些元素不在這個h:form中? – kolobok 2011-05-26 21:01:39

+0

HTML輸入元素像'','