2012-04-08 38 views
0

在我的代码,我有一个PrimeFaces'有几个选项卡精灵组件如下:JSF 2.0:<f:ajax>的侦听器未触发

<h:form id="myForm"> 
    <p:wizard flowListener="#{mrBean.flowControl}" widgetVar="wiz"> 
     <p:tab id="tab1"></p:tab> 
     <p:tab id="tab2"></p:tab> 
     <p:tab id="tab3"> 

      <h:selectOneMenu id="couponList" value="#{mrBean.coupon}" 
          converter="#{codeToCouponConverter}" > 
       <f:ajax listener="#{mrBean.doSomething}" execute="@this"/> 
       <f:selectItem noSelectionOption="true" itemLabel="Choose one..." /> 
       <f:selectItems value="#{mrBean.coupons}" var="c" 
           itemValue="#{c}" itemLabel="#{c.name} - $ #{c.discount}" /> 
      </h:selectOneMenu> 

     </p:tab> 
    </p:wizard> 
</h:form> 

这是托管bean代码:

@ManagedBean(name = "mrBean") 
@ViewScoped 
public class MrBean { 
    private List<Coupon> coupons; 
    private Coupon  coupon; 

    public void doSomething() { 
     System.out.println("DONE"); 
    } 

    public String flowControl(FlowEvent event) { 
     ... 
    } 

    // Getters and Setters 
} 

在标签1中,我有一个<h:selectOneMenu>组件,其中包含一个<f:ajax>标签。我不知道为什么只有当我选择Choose one...选项时才会触发听众。当我从mrBean.coupons列表中选择任何其他选项时,侦听器从不被触发。换句话说,我从来没有看到任何DONE印在控制台上。

* UPDATE ***:这个问题原来从以下Converter现身:

@RequestScoped 
@ManagedBean 
public class CodeToCouponConverter implements Converter { 
    @EJB 
    private MrsBeanInterface mrsBean; 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     String couponCode = value; 
     if (value != null) return mrsBean.getCoupon(couponCode); 
     else return null; 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     if (value != null) { 
      Coupon c = (Coupon) value; 
      return c.getId(); 

     } else return null; 
    } 

    // Getters and Setters 
    public MrsBeanInterface getMrsBean() { 
     return mrsBean; 
    } 

    public void setMrsBean(MrsBeanInterface mrsBean) { 
     this.mrsBean = mrsBean; 
    } 
} 

如果我改变<h:selectOneMenu>如下:

<h:selectOneMenu id="couponList" value="#{mrBean.couponCode}" > 
    <f:ajax listener="#{mrBean.doSomething}" execute="@this"/> 
    <f:selectItem noSelectionOption="true" itemLabel="Choose one..." /> 
    <f:selectItems value="#{mrBean.coupons}" var="c" 
        itemValue="#{c.id}" itemLabel="#{c.name} - $ #{c.discount}" /> 
</h:selectOneMenu> 

和更新mrBean.doSomething功能如下:

@EJB 
private MrsBeanInterface mrsBean; 
private String couponCode; 
private Coupon coupon; 

public void doSomething() { 
    this.coupon = mrsBean.getCoupon(couponCode); 
    System.out.println("DONE"); 
} 

一切正常。

如果你能给我一个解释我做错了Converter的问题,我将不胜感激。

最好的问候,

詹姆斯陈

+0

您是否尝试过在标签中添加event ='change'属性? – 2012-04-08 16:47:58

+0

@djaqeel如果我现在错了,它是默认值。我刚刚尝试过,但不起作用。 – 2012-04-08 16:52:40

+0

只是好奇,试图simlify标签,只显示.name没有美元和其他... – Daniel 2012-04-08 16:57:53

回答

0

使用#{mrBean.doSomething()}用括号或事件参数添加到方法。

相关问题