PrimeFaces p:ajax event =“change”未被触发

时间:2013-04-29 15:55:08

标签: ajax jsf-2 primefaces

当用户在第一个selecOneMenu上触发事件时,我想生成第二个selecOneMenu内容,但它不起作用。

这是face-config.xml中的导航规则

 <navigation-rule>
     <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{contact_.inscription_fournisseur}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>xhtml/Inscription_user.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

这是我的索引页面:当我点击 S'inscrire 时,它会启动第一个列表并将我重定向到Inscription_user.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

  <!--  <link href="${request.contextPath}/css/style.css" rel="stylesheet" type="text/css"/>-->
    <link href="css/style.css" rel="stylesheet" type="text/css"/> 
    <script type="text/javascript" src="js/ao.js" />


    <h:head>
        <title><h:outputText value="#{En_Fr.titre_page_index_fr}"/></title>
    </h:head>
    <body class="box_center" >
        <div style="border:0px #888 solid; margin-top:40px; margin-left:10%; margin-right:10%">


        <div class="box_fournisser">

        <div class="titre_site" style="text-align:right">Accs fournisseur</div>

<div class="pwd_oublie font-label" style="text-align:right; color: #666;">  <label>Mot de passe oublié</label></div><div class="pwd_oublie font-label" style="text-align:right; text-decoration: underline;">  
<label><h:form>
              <h:commandLink action="#{contact_.inscription_fournisseur}"  styleClass="pwd_oublie font-label" style="text-align:left; margin-left: 15px; text-decoration: underline;" >
              S'inscrire
              </h:commandLink>
              </h:form>
              </label>
          </div></div>

    <div  class="statistic_ background_site_unselected  "  ><label class="nombre_statistic">1236</label><label class="label_statistic">appel d'offre</label></div>
    <div  class="statistic_ background_site_unselected  " style="top:10%" ><label class="nombre_statistic">15985</label><label class="label_statistic">demande de devis</label></div>
    <div  class="statistic_ background_site_unselected " style="top:15%" ><label class="nombre_statistic">4859</label><label class="label_statistic">visite par moi</label></div></div>



</body>
        </html>

my Inscription_user.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
       xmlns:p="http://primefaces.org/ui">
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<link href="../css/inscription.css" rel="stylesheet" type="text/css"/>
    <h:head>
        <title><h:outputText value="#{En_Fr.titre_page_index_fr}"/></title>
    </h:head>
    <body>
<div></div>
  <f:view>   
            <h:form>
            <h:messages>d :</h:messages>
          <div   class="ligne_inscription" >
 <div class="label_inscription">Direction</div>
<div>
<p:selectOneMenu effect="fade" value="#{contact_.organisme.selectedDirection}" style="width:110;">
   <f:selectItem itemLabel="Select One" itemValue="0"/>
   <f:selectItems value="#{contact_.organisme.directions}" var="direction" itemLabel="#{direction.direction}" itemValue="#{direction.idDirection}"/>
<p:ajax update="lstfonct" listener="#{contact_.organisme.initFonction}" />
</p:selectOneMenu>
                </div></div>


                           <div   class="ligne_inscription" >
<div class="label_inscription">Fonction</div><div>



<p:selectOneMenu effect="fade" value="#{contact_.organisme.selectedFonction}" id="lstfonct">
  <f:selectItem itemLabel="Select One" itemValue="0"/>
  <f:selectItems value="#{contact_.organisme.fonctions}" var="fonction" itemLabel="#{fonction.fonction}" itemValue="#{fonction.idEmploi}"/>
</p:selectOneMenu>       
</div></div>  


    <h:commandButton type="submit" value="Valider"  action="#{contact_.inscription_choix_alerte()}"  />         

            </h:form>

</f:view>  

    </body>
</html>

这是我的bean Contact_

package beans;
// Generated 23 oct. 2012 21:51:42 by Hibernate Tools 3.2.1.GA


import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import Dao.Emploi_dao;




/**
 * Contact generated by hbm2java
 */

@ManagedBean(name="contact_")
@RequestScoped

public class Contact_  implements java.io.Serializable {


     /**
     * 
     */
    private static final long serialVersionUID = 493917875769565440L;

     @ManagedProperty(value="#{organisme_}")
     private Organisme_ organisme;


    public Organisme_ getOrganisme() {
        return organisme;
    }


    public void setOrganisme(Organisme_ organisme) {
        this.organisme = organisme;
    }




    public String inscription_fournisseur() {
        organisme.setDirections(Emploi_dao.List_direction());
        return "success";
    }



}

我的bean组织_

package beans;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Dao.Activite_dao;
import Dao.Emploi_dao;
import Dao.Organisme_dao;
import hibernate.Activite;
import hibernate.Adresse;
import hibernate.Emploi;
import hibernate.Organisme;



import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.AjaxBehaviorEvent;



/**
 *
 *
 */

@ManagedBean (name="organisme_")
@RequestScoped
public class Organisme_  implements java.io.Serializable  {
     /**
     * 
     */
    private static final long serialVersionUID = 4579411552477526993L;


     private List<Emploi> directions = new ArrayList <Emploi>(0);
     private List<Emploi> fonctions = new ArrayList <Emploi>(0);
     private String SelectedDirection;
     private String SelectedFonction;



     public List<Emploi> getDirections() {
        return directions;
    }



    public void setDirections(List<Emploi> directions) {
        this.directions = directions;
    }



    public List<Emploi> getFonctions() {
        return fonctions;
    }



    public void setFonctions(List<Emploi> fonctions) {
        this.fonctions = fonctions;
    }



    public String getSelectedDirection() {
        return SelectedDirection;
    }



    public void setSelectedDirection(String selectedDirection) {
        SelectedDirection = selectedDirection;
    }



    public String getSelectedFonction() {
        return SelectedFonction;
    }



    public void setSelectedFonction(String selectedFonction) {
        SelectedFonction = selectedFonction;
    }



    public void initFonction()
    {
        System.out.println("------------debut-----------");
         fonctions=Emploi_dao.List_fonction(SelectedDirection);


    }




}

有一个解决方案是从contact_ bean类的inscription_fournisseur()方法中删除方向列表中的初始化,并将其设置在organisation_ bean类的默认构造函数中,在这种情况下它可以工作,但我不知道我更喜欢这个解决方案,我更喜欢第一个场景,请帮助我。

1 个答案:

答案 0 :(得分:2)

  1. 您使用错误的命名空间<f:ajax>应为<p:ajax>

  2. 相反,您应更改update以更新所需内容,并在listener定义的方法中设置所选值,以更改其他selectMenu的数据

  3. 请参阅showcase example