0

我使用了 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
}

再次感谢任何帮助.....

4

1 回答 1

1

值更改侦听器不能用于更改正在更改的值(仅供参考:它们在验证阶段被调用)。看看转换器验证器——它们可以防止垃圾数据进入你的模型。

  /** 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 验证模型,并且必须将验证合并到操作逻辑中。

于 2009-04-29T13:49:42.147 回答