在ManagedProperty中使用ResourceBundle中的属性

我有一个JSF验证器,我正在构建其中包含我希望从ResourceBundle加载的属性。但是,我不太确定如何使用它,因为它没有正确加载。关于如何使这项工作的任何想法? 我尝试使用
@PostContruct
来做,但我在Eclipse中遇到以下错误:   访问限制:类型   PostConstruct因无法访问   对所需图书馆的限制   /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar 所以,我不太清楚最好的方法是什么。我正在谈论的一个样本如下...... 验证者......
@FacesValidator("usernameValidator")
public class UserNameValidator implements Validator {

  @ManagedProperty(value="#{props_userNamePattern}")
  private String userNamePattern;  

  @ManagedProperty(value="#{props_minUserNameLength}")
  private int minUserNameLength;  

  @ManagedProperty(value="#{props_maxUserNameLength}")
  private int maxUserNameLength;

  public void validate(FacesContext context, UIComponent component, Object
        value) throws ValidatorException {
    //My validations here...   
  }

  //Setters for the class properties

}
faces-config.xml中
<resource-bundle>
    <base-name>settings</base-name>
</resource-bundle>
settings.properties
props_userNamePattern = /^[a-z0-9_-]+$/
props_minUserNameLength = 3
props_maxUserNameLength = 30
    
已邀请:
@ManagedProperty
仅适用于
@ManagedBean
课程。
@PostConstruct
也不是您功能需求的正确解决方案。它旨在被放置在一个方法上,该方法将在构造类并且所有依赖注入完成时执行。您遇到的错误是由旧的Eclipse + JRE版本的特定组合引起的。如果升级不是一个选项,您可以通过Window> Preferences> Java> Compiler> Errors / Warnings> Deprecated and restricted API> Forbidden reference> Ignore来禁用警告/错误。 至于你的功能需求,遗憾的是没有任何注释可以实现。但是你可以通过编程方式获得它。
String bundlename = "settings";
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
ResourceBundle bundle = ResourceBundle.getBundle(bundlename, locale);
String usernamePattern = bundle.getString("props_userNamePattern");
// ...
您可以在验证器的构造函数中执行此操作。如果使用得当,无论如何都会为每个视图创建一个新实例。     
添加到BalusC的正确答案;在JSF 2.0 / 2.1验证器中,转换器,PhaseListeners等是一种“二等”公民,因为它们不是注入目标。 这也意味着您无法注入实体管理器或EJB,有时可用于验证目的。 在JSF 2.2中,这应该改变:   所有JSF生命周期工件都应该是   CDI感知和支持   注入/ JSR-299 / JSR-330   (PhaseListeners,NavHandlers,   组件,ActionListeners,   一切。) 请参阅:http://jcp.org/en/jsr/detail?id = 344     
也许接缝面可能有助于这种情况? http://docs.jboss.org/seam/3/faces/latest/reference/en-US/html/artifacts.html http://docs.jboss.org/seam/3/faces/latest/reference/en-US/html/faces.messages.html     

要回复问题请先登录注册