Dynamic JSF resource bundle/message bundle - jsf

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!

Related

Using binding attribute causes javax.faces.FacesException: Cannot find component with identifier

I have a problem I can't quite get a handle on.
First the context: I am developing a web application using Primefaces 3.5 (yes, unfortunately I am stuck with this old version for now), running on JBoss 7.
There is a form with id "form" encompassing all following xhtml code.
I have a component in my view which is provided by usage of the binding attribute:
<p:dashboard id="dashboard" binding="#{myBackingBean.dashboard}" />
Then sometimes I would like to perform an ajax update on this component, this is done by using the RemoteCommand component of primefaces:
<p:remoteCommand
actionListener="#{myBackingBean.someActionListener()}"
process="#this" id="myRmtCmd" oncomplete="myJsFunction();"
update=":form:dashboard" name="myRemoteCommand" />
The RemoteCommand is triggered by a clicking on a Link:
Some Text
This works pretty well so far. However after deploying this code to production I sometimes get a FacesException:
javax.faces.FacesException: Cannot find component with identifier ":form:dashboard"
referenced from "form:myRmtCmd".
This is where my problem lies because I cannot reliably reproduce this exception. My question is this: What could lead to this exception being thrown? It seems to work 95 % of the time but being the perfectionist I am (and many of you reading this are as well, I'm sure ;) ) I would like this code to work 100 % of the time. What am I missing?
Before answering please consider these constraints:
yes, i have to use the binding attribute for providing the dashboard as I need a great deal of control over what gets added to the component
to avoid using IDs I also tried updating the dashboard by its css class via one of primefaces' advanced selectors: #(.ui-dashboard) - this also does not work!
yes, it would be possible to use a commandbutton/link instead of wiring up the remotecommand component to a simple html link but in this case the link is rendered by a JSF renderer component and I made some bad experiences with dynamically adding buttons etc (due to JSF Spec Issue 790)
Cheers,
p.s.
I also had this weird behavior.
There are probably more than one component bindded to #{myBackingBean.dashboard}, so the first one sets the id and there will be no one called "dashboard".

Different ways of declaring a resource bundle in JSF2.x

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...

Managed beans in xsp-config files

Can anyone tell me if it is possible to keep managed beans in separate .xsp-config configuration files?
And if yes than how to do this? I have tried and it only worked if I put them into faces-config.xml file.
If you want to split your bean definitions into multiple configuration files, you'll need to create an OSGi plugin. XSP libraries can contain more than one faces-config file (and you can name them whatever you want, because you specify in the library class which XML files contain faces-config definitions). But in an NSF, you're limited to just the auto-generated faces-config.xml file.
Yes, Managed beans must be defined in the faces-config.xml file.
For a good reference on all the different options within faces-config and xsp-config take a look at this website. It describes the format of pretty much everything you might ever want to add to a faces-config or xsp-config and a brief description of the options.
As far as I know they have to be in the faces-config.xml
faces-config is a JSF implementation, you should check out the JSF specification for this, there are even some good post in this forum like this one:
JSF faces config file outside WEB-INF?

JSF 2.0 : Runtime variables in property files

All text in our app is read from properties file
e.g.
<h:outputText value="#{text['fill.form']}"/>
text is a resource bundle that resolves to Text.properties.
Text.properties
fill.form=Please fill out the form below
There are other instance where we need run time variables inside the properties
e.g.
welcome.user=Welcome #{name.last}, #{name.first} to our site
name is a managed bean whose properties first and last get resolved at run time.
However the first name and last name are not resolved at run time. Instead the user sees the following
Welcome #{name.last}, #{name.first} to our site
Any solutions to this problem?
Should I write a custom EL resolver to resolve the text from the properties and also any run time properties inside of it.
I believe you want to use h:outputFormat to accomplish this. In your example, you might do this:
<h:outputFormat value="#{text['welcome.user']}">
<f:param value="#{name.last}"/>
<f:param value="#{name.first}"/>
</h:outputFormat>
For this to work, you need to change your welcome message to this:
welcome.user=Welcome {0}, {1} to our site

How many ways there are to declare variables in facelets?

I noticed that c:set does not work well used inside "include of include of include", as important notice facelets documentation does't recommend it too.
Now I am using ui:param inside ui:include, but it is a bit dispersive when no attached notes about params comes with the include, is there something other way to declare "global vars"?
This is really a matter of trying to fit old JSP programming into the JSF framework. You should be using backing beans to hold your data.
If you try to hard-code data directly into your xhtml file, you are defeating the purpose of JSF's MVC framework. If you have a specific example of what you are trying to do, I could give you a specific recommendation.

Resources