关于Icefaces和valueChangeListener的问题

时间:2009-04-29 12:39:59

标签: jsf icefaces

我使用icefaces 1.7.1,我使用ice:inputText和valueChangeListener,如下所示:

<ice:inputText value="#{myBean.name}" valueChangeListener="#{myBean.nameChangedListener}"/>

在MyBean.java中我有:

public void nameChangedListener(ValueChangeEvent event){
   // test the new value : if it's ok continue but if it is not ok i need it to keep the old value.
   // I know that the valueChangeListener invoked before the old value is replaced by the newValue, is it ok?, and if ok : what to do to keep the oldValue if the newValue is worng
}

再次感谢您的帮助.....

1 个答案:

答案 0 :(得分:1)

值更改侦听器不能用于更改要更改的值(仅供参考:它们在验证阶段调用)。查看convertersvalidators - 它们可以防止垃圾数据进入您的模型。

  /** validator="#{myBean.checkThreeCharsLong}" */
  public void checkThreeCharsLong(FacesContext context,
      UIComponent component, Object value) {
    boolean failedValidation = (value == null)
        || (value.toString().trim().length() < 3);
    if (failedValidation) {
      ((EditableValueHolder) component).setValid(false);
      // message to user
      String clientId = component.getClientId(context);
      FacesMessage message = new FacesMessage(
          "value should be at least three characters long");
      context.addMessage(clientId, message);
    }
  }

使许多人绊倒的一个方面是,包含无效数据的提交表单将阻止触发操作。这是设计 - 它可以防止业务逻辑对坏数据进行操作。如果您需要触发操作,即使请求中存在无效数据,也无法使用JSF验证模型,并且必须将验证合并到您的操作逻辑中。