在ManagedProperty中使用ResourceBundle中的Properties

mik*_*r87 4 jsf resourcebundle postconstruct jsf-2

我有一个JSF验证器,我正在构建其中包含我希望从ResourceBundle加载的属性.但是,我不太确定如何使用它,因为它没有正确加载.关于如何使这项工作的任何想法?

我尝试使用a @PostContruct来做,但我在Eclipse中遇到以下错误:

访问限制:由于对所需库/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar的限制,无法访问PostConstruct类型

所以,我不太清楚最好的方法是什么.我正在谈论的一个样本如下......

验证者......

@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

}
Run Code Online (Sandbox Code Playgroud)

faces-config.xml中

<resource-bundle>
    <base-name>settings</base-name>
</resource-bundle>
Run Code Online (Sandbox Code Playgroud)

settings.properties

props_userNamePattern = /^[a-z0-9_-]+$/
props_minUserNameLength = 3
props_maxUserNameLength = 30
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

@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");
// ...
Run Code Online (Sandbox Code Playgroud)

您可以在验证器的构造函数中执行此操作.如果使用得当,无论如何都会为每个视图创建一个新实例.