【问题标题】:Spring, numeric validation, default value, warning messageSpring,数值验证,默认值,警告信息
【发布时间】:2013-01-24 19:02:32
【问题描述】:

我有 bean 定义,例如

<bean id="DsdDetectorLogic" class="my class" init-method="init" lazy-init="true" >
    <property name="threshold" value="#{ properties.threshold }" />
    <property name="lag" value="#{ properties.lag }" />
    ...
</bean>

我需要对特定参数(lag)添加约束,如果此参数超过最大值 3 或最小值 1 ,我需要它来获得默认值 1。另外我需要收到一些警告消息,表明此参数收到了一个默认值作为结果。

我熟悉使用 javax.validation.constraints.Min / Max 对代码中的字段进行注释的解决方案。

是否可以使用一些spring特性来编辑xml文件,或者唯一的解决方案是在调用setter时从java对象类中进行编辑?

【问题讨论】:

  • 查看static.springsource.org/spring/docs/1.2.9/reference/beans.html 我没有看到任何关于您的要求的信息。但是,第 3.3 节提到了设置构造函数值。如果这是您的自定义类,您也许可以通过这种方式实现类似的效果。
  • @Jesse J:我找到了一种方法来部分解决我的问题,您能帮忙找到如何显示警告消息吗? (附上我的答案)

标签: java spring constraints default-value


【解决方案1】:

我将部分回答我的问题,因为我发现了如何检查约束并通过 xml 发送默认值,警告消息仍然是一个难题。

我将使用 SPeL:

<bean id="DsdDetectorLogic" class="my class" init-method="init" lazy-init="true" >
    <property name="threshold" value="#{ properties.threshold }" />
    <property name="lag" value="#{ (1 > new Integer(properties.lag ) or 
                                    new Integer(properties.lag ) > 3) ? 1 : 
                                    properties.lag  }" />
    ...
</bean>

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 2017-08-17
    相关资源
    最近更新 更多