Bean验证值

时间:2013-11-24 05:56:51

标签: jsf

我正在尝试使用bean验证进行实验。以下代码片段:     

    <h:inputText id="id1" value="#{hello.sur}"/>
    <h:commandButton id="submit" value="Submit" 
                     action="response">
    </h:commandButton>

    <h:commandButton id="reset" value="Reset" type="reset">
    </h:commandButton>
</h:form>
<h:form>
    <h:inputText id="id2" value="#{hello.sur}"/>
</h:form>

private String surHello的{​​{1}}属性注释@DecimalMin("5.00") 当我传递id1值大于5id2为空时我没有错误。为什么?但如果id1id2都是epmty我就会有错误。

1 个答案:

答案 0 :(得分:0)

您的问题/代码似乎存在一些不平衡:首先,属性sure应该具有数字类型,而不是字符串,其次,您提供的值大于5,这意味着验证(如果执行)将不会被触发(没有错误消息)。 此外,虽然这是一个实验,但您可以省略<commandButton />并且应该创建托管bean @ViewScoped,以便只要您在同一页面中执行测试,这就是原因,使用<f:ajax>标记,以便在运行时刷新视图中的某些片段(组件)而无需刷新所有页面(发送请求),这对转换/验证很有用。为了更好地练习,请进行这些更改并提供小于5的值:

<h:form> 
       <h:inputText id="id1" value="#{hello.sur}">
           <f:ajax event="blur" render="id1Message"/>
       </h:inputText>
       <h:message for="id1" id="id1Message"/>

       <h:inputText id="id2" value="#{hello.anotherProperty}">
           <f:ajax event="blur" render="id2Message"/>
       </h:inputText>
       <h:message for="id2" id="id2Message"/>    
</h:form>