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?
Related
This question already has an answer here:
What is the use of faces-config.xml in JSF 2?
(1 answer)
Closed 4 years ago.
I have been studying JavaEE8 and practicing with some projects, understanding the new technologies like Servlet 4.0 and JSF 2.3. I have read in many forums and pages, in some I see that they say that web.xml and faces-config.xml are not necessary, because annotations are now used, but in some others they continue to use them.
In which cases should you continue to use the Web Deployment Descriptor-> web.xml and the application configuration resource file-> faces-config.xml?
They aren't required, but I'd highly recommend to use them since you'll need them sooner or later for additional configuration anyways.
The annotations are used to achieve better readability and to simplify the faces-config.xml and web.xml, however they do not even nearly allow for the configuration options that can be made in the configuration files.
For example you can use the #FacesValidator Annotation instead of declaring and referencing the corresponding class in the faces-config.xml.
A task that can't be achieved by using annotations would be the declaratation of a welcome page. If you want to specify it, you need the web.xml.
I found a specific problem in the Mojarra implementation of JSF 2.3 when using the web.xml or faces-config.xml file. In the code of this implementation, the ELUtils class has the following condition:
if (getFacesConfigXmlVersion(facesContext).equals("2.3") || getWebXmlVersion(facesContext).equals("4.0")) {
throw new FacesException("Unable to find CDI BeanManager");
}
which throws an exception: "Unable to find CID BeanManager". I only had the faces-config.xml file with the latest version of JSF specified in the namespace and I was throwing that exception.
To avoid this problem, you can specify a different version of JSF (before 2.3) in the faces-config.xml file and specify a different version of the web.xml file (before 4.0), or simply do not add any of these configuration files. In my case, I removed the faces-config.xml and ran the application without problems.
I hope that the implementation of Mojarra will solve that little detail.
Short Version:
If two JSF library jar files both include a custom renderer for the same family and type, is there any way from within the library itself (i.e. not from the containing app) of specifying which one should be used? Something like assigning a priority, with higher ones used in preference to lower ones?
Longer Version:
I'm using Primefaces in a project and trying to override the provided head renderer with my own:
<render-kit>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>javax.faces.Head</renderer-type>
<renderer-class>com.example.MyHeadRenderer</renderer-class>
</renderer>
</render-kit>
If I put that into the WEB-INF/faces-config.xml of the war file then it's all good, and my renderer gets used.
However, if I try to deploy my code as part of a library jar (my-utils.jar), with the renderer defined in META-INF/faces-config.xml, then the Primefaces one is used. This contains exactly the same definition, so I'm guessing it just depends on the order they get loaded. Indeed, renaming my library to "xx-comps.jar" works, so it would appear that JSF is loading faces-config.xml files from all jar files in alphabetical order, with later entries just overwriting earlier ones.
Is there any way of forcing the selection to my library?
So far, I have these options:
Put my renderer directly into the WEB-INF/faces-config.xml of the war.
Build a custom Primefaces jar with that one renderer definition
removed.
Rename my library and rely on some (undocumented as far as I can
see) behaviour from the JSF loader.
Add a custom renderkit which extends the standard one, and reference
that from my war WEB-INF/faces-config.xml.
The first three all work but are not ideal, as (1) and (2) require changes outside my library, and (3) just looks dodgy as hell....
The fourth is just an idea as I've never written a render kit before so not aware of the effort involved. No idea if it is practical or would work, but it is better than (1) because at least the application only references a single render kit, and does not need to be updated if/when new renderers are added. Happy to put more effort into researching this approach if it seems a reasonable solution.
Also, I'd ideally prefer to use annotations rather than XML:
#FacesRenderer(componentFamily = "javax.faces.Output", rendererType = "javax.faces.Head")
public class MyHeadRenderer extends Renderer {
...
}
Thanks
You can specify the ordering via <ordering> in faces-config.xml of the JAR.
E.g. if you want your utility library to be loaded after all others, then just do so:
<ordering>
<after>
<others />
</after>
</ordering>
If you want to force an explicit ordering, then hook on specifically PrimeFaces, which has a <name>primefaces</name> in its faces-config.xml:
<ordering>
<after>
<name>primefaces</name>
</after>
</ordering>
I had a managed bean called controlBean and i deleted it. Then i tried to add a new bean with the same name. I also had deleted it from faces-config.xml file, which was looking like this:
<managed-bean>
<managed-bean-name>controlBean</managed-bean-name>
<managed-bean-class>com.app.managedbeans.ControlBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
The problem is, even though i specify its name as controlBean again, NetBeans calls it controlBean1, and if i delete and add again it is called controlBean2 etc. Does anyone know the reason? What can i see to fix it?
Thanks
Please check you have added a default constructors for other managed beans.
I had the same issue with Netbeans 7.3. I have used annotations instead in that case. I added some methods to the few other beans. Stopped the GlassFish server. Deleted the dis and build folders manually. Clean and build the project again. Ant that error was not there for the time being. Don't know the exact cause. I had to do this few times for other beans as well.
If still not response, create a new bean and let it be there until a proper solution is available.
Get rid of all this XML and use the #ManagedBean annotation.
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!
I am new to JSF. I do not understand how to create my own messages properties files and where exactly to put this file. Can anyone help me with this?
I am not getting how to create new my own properties file
Create a text file according the java.util.Properties contract. ISO-8859-1 encoded text with key=value pairs, each in its own line. In case of JSF message resource, you can use the key names as described in the JSF specification to override default messages. You can find them in chapter 2.5.2.4 of the JSF specification (here are the JSF 1.2 specification and JSF 2.0 specification).
and where to put this file under directory
Just put it in the classpath as you do with your normal Java classes.
The <message-bundle> in faces-config.xml file should refer to the full qualified pathname of the resource. If you've named the properties file Messages.properties and just dropped in the root of the classpath, then its declaration should look like
<application>
<message-bundle>Messages</message-bundle>
</application>
But if you've dropped it in for example the package com.example.i18n, then it should look like:
<application>
<message-bundle>com.example.i18n.Messages</message-bundle>
</application>