Using properties from ResourceBundle in ManagedProperty

I have a JSF Validator that I create that has properties that I would like to load from a ResourceBundle. However, I'm not quite sure how this works, as it does not load properly. Any ideas on how I can make this work?

I tried using @PostContruct to do this, but I get the following error in Eclipse:

Access restriction: the PostConstruct type is not available due to the restriction on the required library / System / Library / Java / JavaVirtualMachines / 1.6.0.jdk / Contents / Classes / classes.jar

So I'm not too sure how this works best. An example of what I'm talking about below ...

Validator ...

 @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 
+4
source share
3 answers

@ManagedProperty works on @ManagedBean only classes. @PostConstruct also not be the right solution for your functional requirement. It intends to be placed in a method that must be executed when the class has been built, and all dependency injections have been completed. The error you are encountering is caused by a specific combination of older versions of Eclipse + JRE. If updating is not an option, you can disable the warning / error using the window> Settings> Java> Compiler> Errors / Warnings> Deprecated and restricted API> Forbidden link> Ignore.

As for your functional requirement, unfortunately, no annotation that achieves this comes to mind. However, you can get it programmatically.

 String bundlename = "settings"; Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); ResourceBundle bundle = ResourceBundle.getBundle(bundlename, locale); String usernamePattern = bundle.getString("props_userNamePattern"); // ... 

You can do this in the validator constructor. When used correctly, in any case, a new instance will be created for each species.

+6
source

Adding to the correct answer BalusC; In JSF 2.0 / 2.1 Validators, Converters, PhaseListeners, etc. They are a kind of second-class citizens, since they are not the targets of the injection.

It also means that you cannot enter an entity or EJB administrator, which can sometimes be used for validation purposes.

In JSF 2.2, this should change:

All JSF life cycle artifacts should be CDI Support and Injection Support / JSR -299 / JSR-330 (PhaseListeners, NavHandlers, Components, ActionListeners, all.)

See: http://jcp.org/en/jsr/detail?id=344

0
source

Source: https://habr.com/ru/post/1347551/


All Articles