I am working on JSF2.2 at my work place. My faces-config.xml has the resource bundle tag which helps me to assign a variable to a property file and use the variable in a EL.
<resource-bundle>
<base-name>properties.common</base-name>
<var>prop</var>
</resource-bundle>
I also found another way of achieving this by using the f:loadBundle tag like this
<f:loadBundle basename="properties.common" var="prop"/>
But this is a localized solution, meaning I would have to write this in every page.
Would this work if i define this in a template ? If yes, how do i achieve it.
Is there any other way that i can declare the resource globally with a variable to be used in an EL(like in the case of faces-config.xml)
Would this work if i define this in a template ?
Yes.
If yes, how do i achieve it.
Just do exactly as you said. Define it in a template.
Is there any other way that i can declare the resource globally with a variable to be used in an EL(like in the case of faces-config.xml)
Put it in request map yourself in (post)constructor of a request scoped bean which is referenced in the view.
ResourceBundle bundle = ResourceBundle.getBundle("properties.common", facesContext.getViewRoot().getLocale());
externalContext.getRequestMap().put("prop", bundle);
It can even be referenced as a property of a request scoped bean and this guarantees construction of the bean even if it's not referenced elsewhere in the view.
This is exactly what Iam looking for, see my post https://stackoverflow.com/questions/24461307/jsf-2-external-resource-bundle
ResourceBundle bundle = ResourceBundle.getBundle("properties.common",facesContext.getViewRoot().getLocale());
externalContext.getRequestMap().put("prop", bundle);
thanks all...
Related
is it possible to change the default scope of CDI Beans (dependent scope) to something else (like ApplicationScope)?
Thanks
There is no simple API call or configuration property to do that, but you can always write a portable extension to add or replace scope annotations by observing the ProcessAnnotatedTypeevent.
I've got to create screens to display a lot of JPA entities in the View. It would be great to create one facelet and pass to it a collection of fields e.g. List<Object>.
The facelet/custom component would need to convert each element of the list into the appropriate tag for display e.g. an enum field to h:selectOneMenu, String field to h:inputText, etc. This would need to be done at run time.
What's the easiest way to do this?
Worked on a project previously that created entire pages dynamically from stored configuration. There are two basic things you need
A BackingBean. You'll used this to get access to the UIComponent on the facelet which will act as the parent to the generated UIComponents. Something like a panelGroup. But, you'll need to bind the UIComponent to the backing bean, in order to have a parent against which you'll add the dynamically-created UIComponents
Access to the Application component. Typically FacesContext.getApplication() (I worked on this in JavaEE 5, so it might look a little different with injection). Once you have the Application component, you call the createComponent method, passing in the type of component you want to create.
It then becomes an activity of creating components dynamically, configuring them in code and adding them to the parent UIComponent defined via a binding bean. It can be tricky, but it can be done.
As described in this question I try to perform some field validation in a form on the backing bean side. For this I would like to access the violating fields to mark them.
From searching the web there seem to be two ways to do this:
store the components in the backing bean for access and use them in the JSF pages via the binding attribute.
Use standard value binding in the JSF pages and when needing access to a component from the bean, look it up via UIViewRoot.findComponent(String id)
As far as I can see both ways have drawbacks:
Component bindings blows up the backing bean with variables and getters/setters, some sites strongly discourage the use of component binding at all. In any case, a request scope is advised. On the other hand, findComponent() always traverses the tree, which may or may not be costly, right? (Plus, at the moment I can't find my component at all, but that is another problem)
Which would be the way to go? Are these interchangeable alternatives and if not, based on what criteria do you chose? Currently I just don't have enough insight to make a decent decision...
First of all, regardless of the choice, both are a poor practice. See also How does the 'binding' attribute work in JSF? When and how should it be used?
If you had to make the choice, component bindings are definitely faster and cheaper. It makes logically completely sense that a tree scan as done by UIComponent#findComponent() has its performance implications.
Indeed, the backing bean holding the component bindings must be request scoped, but you could easily inject a different scoped backing bean holding the business logic in it by #ManagedProperty.
A cleaner approach would be to use a Map as holder of all component bindings. You only need to add the following entry to faces-config.xml:
<managed-bean>
<managed-bean-name>components</managed-bean-name>
<managed-bean-class>java.util.HashMap</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
This can just be used as
<h:inputSome binding="#{components.input1}" />
<h:inputSome binding="#{components.input2}" />
<h:inputSome binding="#{components.input3}" />
And this can be obtained in other beans as
Map<String, UIComponent> components = (Map<String, UIComponent>) externalContext.getRequestMap().get("components");
This way you don't need to worry about specifying individual properties/getters/setters. In the above example, the Map will contain three entries with keys input1, input2 and input3, each with the respective UIComponent instance as value.
Unrelated to the concrete question, there may be a much simpler solution to the concrete problem as you described in the other question than performing the validation in the action method (which is actually Bad Design). I've posted an answer over there.
Is there any chance to get the html content (mime) from a rich text component without any datasource. I would like to grab the content from the field like this. getComponent("FieldName").value but this dosen't work.
thanks.
You can bind the control to a scoped variable; for example, #{viewScope.comments}. You can then retrieve the submitted value from the scope instead of from the component itself; for example, viewScope.get("comments").
Alternatively, you can set a dataContext variable to a JS expression, e.g. <dataContext var="richText" value="#{javascript:return {value:""};}" />. Then you can bind the control to #{richText.value} and retrieve it via the same expression.
And, of course, you could define a managed bean and bind the control to one of its properties. This option provides the most flexibility, but isn't quite as simple as the other two options above.
The solution for my problem is
getComponent("FieldName").getValue()
thanks for your help.
I would like to have a dynamic resource bundle in my application. I will show a form to the user where he can edit the value of Resource Bundle. I can’t restart my application for this changes take effect. I found a solution that solves part of my problem. Using “commons-configuration” from Apache or/and this http://www.coderanch.com/t/292347/JSP/java/we-reload-property-file , i could change my “.properties” file and get the results using “ResourceBundle.getBundle” .
The problem is when I try to access the property in my XHTML file like this:
<h:outputLabel value="#{msg[user.name]}" />
The value of this key is out of date.
Is there a solution for this?
Just for info, I am using spring, so I have this in my faces-config:
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
But I don’t think this is the problem. Anyway, another solution would be to use a session bean and get the property values accessing it.
What do you think about it? Would be better store this values in my database and forget about the properties file?
\o/
I have tested to move my resource bundle definition from my faces-config to my page using tag f:loadBundle and it worked!