5
votes

Ok i've already browsed a lot of topics here but none of them solved my problem. My problem is simple: I have ultra simple custom component:

@FacesComponent("HelloWorldComponent")
public class HelloWorldComponent extends UIComponentBase {

public String getFamily() {
return "my.custom.component";
}

@Override
public void encodeBegin(FacesContext ctx) throws IOException {
String value = (String) getAttributes().get("value");

if (value != null) {
ResponseWriter writer = ctx.getResponseWriter();
writer.write(value.toUpperCase());
}
}
}

but unfortunately i get error:

JSF1068: Cannot instantiate component with component-type HelloWorldComponent

My taglib:

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 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-facelettaglibrary_2_0.xsd" version="2.0">

<namespace>http://example.com/test</namespace>
<tag>
<tag-name>helloWorldComponent</tag-name>
<component>
<component-type>HelloWorldComponent</component-type>
</component>
</tag>
</facelet-taglib>

The key thing is if i place my custom component in faces-config everything works perfect, but as a very stubborn greenhorn in JSF im convinced that there has to be way to make the annotations work without registering custom components in faces-config. Im expecting to have A LOT of custom components so i want to know how to make them work with pure annotations so as not to write milion lines of code in faces-config.

<?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">

<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

<!--    <component> -->
<!--         <component-type>HelloWorldComponent</component-type> -->
<!--         <component-class>com.first.utils.view.components.HelloWorldComponent</component-class> -->
<!--     </component> -->

</faces-config>

When i uncomment the component tag, everything works. But literally dont want to do that, its not laziness its forcing computers to do what I WANT ;).

My structure of project: (im using spring & maven)

src  
~main  
~~java  
~~resources  
~~~META-INF  
~~~~faces-config.xml (duplicated as i read other topics)  
~~~~persistence.xml  
~~webapp  
~~~resources  
~~~~css  
~~~~utilComponent  
~~~WEB-INF  
~~~~templates  
~~~~faces-config.xml  
~~~~my.taglib.xml  
~~~~web.xml  
~~~*.html webpages..  

Topics i've already browsed: JSF custom component is not found
Cannot find annotated custom JSF2 component in jar
Custom Component using JSF

It would be ideal if i wouldnt have to define my taglib but i guess it is impossible? But first thing first i want to make annotations work... any help appreciated

2
I don't do Maven, but the component class file must be in /WEB-INF/classes or in a JAR file in /WEB-INF/lib (containing /META-INF/faces-config.xml) in order to get @FacesComponent to work. Doublecheck the Maven-produced WAR file and alter build configuration if necessary.BalusC
my component class (HelloWorldComponent) is in WEB-INF/classes along with normal classes from my project, and im stuck cuz i've tried all the hints/solutions i could find in the webZen

2 Answers

0
votes

Have you tried restarting the application server? I am not sure how it is with Spring, but solved a similar problem for me in JBoss. It is possible that components are registered automatically when the server is starting. Without restart, they have to be listed in some file.