简体   繁体   中英

JSF 2 Custom component annotation does not work

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

Try to put the my.taglib.xml file into META-INF directory.

http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=customJsfComponent

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM