如何使用价值变化监听器 [英] how to use value change listener

查看:137
本文介绍了如何使用价值变化监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何处理f:valueChangeListener,我想选择国家和资本出现,所以这是我的代码,但它不工作什么是错过或什么是错的?

I don't know how to deal with f:valueChangeListener , I want to select country and the capital appear so this is my code but it doen't work what is miss or what is the wrong?

 Country:
  <h:selectOneMenu value="#{event.country}" onchange="submit()">
        <f:valueChangeListener type="org.jsf.model.ValueListener"/>
        <f:selectItems value="#{event.countries}"/>     
  </h:selectOneMenu>
 Capital: #{event.capital}  

我的托管bean

public class EventsBean{

private String capital;
private String country;
String countryCapital;


private String [] countries = {"Select","Egypt","United States","Kuwait"};

public String[] getCountries() {
    return countries;
  }

// getters and setters
}

实现ValueChangeListener的类

The class that implements ValueChangeListener

package org.jsf.model;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;

public class ValueChangeClass implements ValueChangeListener {
String capital;
 @Override
   public void processValueChange(ValueChangeEvent event)throws AbortProcessingException {

if ("Egypt".equals(event.getNewValue())                  capital = "Cairo";
else if ("Kuwait".equals(event.getNewValue()))           capital = "Kuwait";
else if ("United States".equals(event.getNewValue()))    capital = "Washington";
else   capital = "";


new EventsBean().setCapital(capital);
}
}

它不起作用
这是新的EventsBean()。setCapital(capital);对吗?

It doesn't work ! Is this new EventsBean().setCapital(capital);right ?

推荐答案


这是新的EventsBean()。setCapital(capital) / p>

Is this new EventsBean().setCapital(capital);right ?

不,这不对,你手动创建一个全新的实例,而不是使用由JSF管理的一个实例一旦方法完成并返回,实例将完全消失,您应该将资本设置为由JSF管理的实例。有几种方法可以实现这一点。如果您真的打算以这种方式使用 ValueChangeListener ,那么您需要如下修复:

No, it is not right. You're manually creating a brand new instance instead of using the one which is managed by JSF. Your instance would totally disappear once the method finishes and returns. You should instead be setting the capital in the instance which is managed by JSF. There are several ways to achieve this. If you really intend to use the ValueChangeListener this way (which is rather strange for this particular purpose by the way), then you need to fix it as follows:

FacesContext context = FacesContext.getCurrentInstance();
EventsBean eventsBean = context.getApplication().evaluateExpressionGet(context, "#{event}", EventsBean.class);
eventsBean.setCapital(capital);

更容易在 EventsBean 本身。

<h:selectOneMenu value="#{event.country}" valueChangeListener="#{event.changeCountry}" onchange="submit()">
    <f:selectItems value="#{event.countries}"/>     
</h:selectOneMenu>
Capital: #{event.capital}





private String country;
private String capital;
private Map<String, String> capitals;

@PostConstruct
public void init() {
    capitals = new HashMap<>();
    capitals.put("Egypt", "Cairo");
    capitals.put("Kuwait", "Kuwait");
    capitals.put("United States", "Washington D.C.");
}

public void changeCountry(ValueChangeEvent event) {
    capital = capitals.get(event.getNewValue());
}

或者,由于您已经在使用JSF 2.0,更好的是使用< F:AJAX> 。它在正确的时刻做正确的工作。 (Ab)使用 valueChangeListener 在原始代码中的方式实际上是JSF 1.x时代的一个缺点。

Or, since you're already using JSF 2.0, much better is to use <f:ajax>. It does the right job at the right moment. (Ab)using the valueChangeListener the way as in your original code is actually a leftover of the JSF 1.x era.

<h:selectOneMenu value="#{event.country}">
    <f:selectItems value="#{event.countries}"/>     
    <f:ajax listener="#{event.changeCountry}" render="capital" />
</h:selectOneMenu>
Capital: <h:outputText id="capital" value="#{event.capital}" />





// ...

public void changeCountry() {
    capital = capitals.get(country);
}



另请参见:




  • 何时使用valueChangeListener或f:ajax听众?

  • See also:

    • When to use valueChangeListener or f:ajax listener?
    • 这篇关于如何使用价值变化监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆