简体   繁体   English

外部化/集中化JSF2中的验证属性

[英]Externalize / centralize validation properties in JSF2

This is an example of an input field in JSF having the validateLength validator. 这是JSF中具有validateLength验证器的输入字段的示例。

<p:inputText id="priceModelName" size="30"
    value="#{createPriceModelBackingBean.priceModel.priceModelName}"
    required="true" label="#{labelResource.priceModelName}">
    <f:validateLength minimum="3" maximum="45" />
</p:inputText>

The priceModelName input field is repeated in another xhtml (create.xhtml and edit.xhtml), i would like to have the values for minimum and maximum externalized / centralized so that it is not repeated. priceModelName输入字段在另一个xhtml(create.xhtml和edit.xhtml)中重复,我希望minimummaximum外部化/集中化值,以便不重复该值。 Is there a good recommendation for such a use case? 对于这种用例是否有很好的建议?

I was thinking of probably using a properties file. 我在考虑可能使用属性文件。

<f:validateLength minimum="#{validationResource.priceModel.minimum}" maximum="#{validationResource.priceModel.minimum}" />

Apart from the way you found out yourself, another ways are: 除了您发现自己的方式之外,还有其他方法是:

  1. Extend the JSF LengthValidator and set the defaults in the constructor. 扩展JSF LengthValidator并在构造函数中设置默认值。

     @FacesValidator("priceModelNameLengthValidator") public class PriceModelNameLengthValidator extends LengthValidator { public PriceModelNameLengthValidator() { setMinimum(3); setMaximum(45); } } 

    (make those magic numbers if necessary constants or configuration settings) (如有必要,请对那些魔术数字进行常量或配置设置)

    and reuse it 并重复使用

     <f:validator validatorId="priceModelNameLengthValidator" /> 

  2. Use JSR 303 bean validation. 使用JSR 303 bean验证。

     @Size(min=3, max=45) private String priceModelName; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM