Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0 Converter not working from separate jar-file

I have a custom converter to select a Country in a SelectOneMenu component:

File: address.jar

@FacesConverter(value="CountryConverter", forClass=Country.class)
public class CountryConverter implements Converter {

private CountryBean countryBean = CountryBean.getCountryService();

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return countryBean.find(value);
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value != null)
        return ((Country)value).getcc_fips();
    else
        return null;
}    

And this is the xhtml text:

File: Project root

  <h:selectOneMenu id="country" value="#{cc.attrs.addrEntity.country}">
     <f:selectItem itemLabel="Please select one..." 
             noSelectionOption="true" />
     <f:selectItems value="#{cc.attrs.addrBean.countries}"
                    var="model"
                    itemLabel="#{model.name}"
                    itemValue="#{model}"
                    noSelectionValue="&#8220;no selection&#8221;"/>
     <f:converter ConverterId="CountryConverter"/>        
 </h:selectOneMenu>

I have the converter in a file "address.jar" and when I try to open the page to write the address, then it responds saying "Expression Error: Object with name MyCustomCoverter not found.". Even thought when I copy the converter to the project where the xhtml files are, then it works ok. What can I do to solve this?

Why it doesn't work from a separated jar file?

Thanks.

like image 969
Joe Almore Avatar asked Jan 16 '12 20:01

Joe Almore


1 Answers

You have to supply a JSF 2.0 compatible /META-INF/faces-config.xml file in the JAR file in order to get JSF to auto-scan the JAR file for classes with JSF annotations.

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
</faces-config>

Without that file, JSF won't auto-scan the JAR file to save performance and thus your @FacesConverter won't be found nor registered.

like image 150
BalusC Avatar answered Oct 13 '22 08:10

BalusC