复合组件editableValueHolder与Bean验证不一致

时间:2012-01-03 12:17:48

标签: validation jsf-2 composite-component

我有以下复合组件:

<!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:composite="http://java.sun.com/jsf/composite" 
>
<composite:interface>
  <composite:attribute name="value"/>
  <composite:editableValueHolder name="value" targets="#{cc.clientId}:value"/>
</composite:interface>

<composite:implementation>
  <h:inputText id="value" />
  <h:message for="#{cc.clientId}:value"/>
</composite:implementation>

</html>

以下托管bean

import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.validation.constraints.NotNull;

@ViewScoped
@ManagedBean
public class TestMB {

    @NotNull
    private Date value;

    public String action1() {
        System.out.println("The value is: " + value);
        return null;
    }

    public Date getValue() {
        return value;
    }

    public void setValue(Date value) {
        this.value = value;
    }

}

以下自定义转换器:

import java.util.Date;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("myCustomConverter")
public class MyCustomConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
        if (value == null || "".equals(value)) {
            return null;
        }
        return new Date();
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
        if (obj == null) {
            return null;
        }
        return obj.toString();
    }
}

以下测试页:

<h:body>
  <h:form>
    Enter value: 
    <wui:test value="#{testMB.value}" id="myID">
      <f:converter converterId="myCustomConverter" for="value"/>
    </wui:test>
    <h:commandLink action="#{testMB.action1}" value="submit"/>
  </h:form>
</h:body>

当我运行Web应用程序并将文本字段留空并点击提交按钮时,不会引发验证错误,我在Java控制台上获得以下内容:

The value is: null

为了使事情变得更复杂,如果我按如下方式修改我的复合组件(我将值=“#{cc.attrs.value}”添加到h:inputText

<!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:composite="http://java.sun.com/jsf/composite" 
>
<composite:interface>
  <composite:attribute name="value"/>
  <composite:editableValueHolder name="value" targets="#{cc.clientId}:value"/>
</composite:interface>

<composite:implementation>
  <h:inputText id="value" value="#{cc.attrs.value}"/>
  <h:message for="#{cc.clientId}:value"/>
</composite:implementation>

</html>

在设置NO值时再次尝试提交表单,我没有在页面上显示任何验证错误,但我在控制台中收到以下警告:

INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=myID:value[severity=(ERROR 2), summary=(may not be null), detail=(may not be null)]

然后,如果我再次尝试重新提交表单,即使字段为空,也会提交操作。我在控制台中获得以下内容:

The value is: null

但是,如果我删除 id =“myID”一切正常!

最后:如果我在表单上设置prependId =“false”,一切正常。

但我不想设置prependId =“false”,我确实需要设置ID:(

0 个答案:

没有答案