java - JSF 2 自定义组件注释不起作用

标签 java spring jsf annotations

好的,我已经在这里浏览了很多主题,但没有一个能解决我的问题。我的问题很简单:我有超简单的自定义组件:

@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());
}
}
}

但不幸的是我得到了错误:

JSF1068: Cannot instantiate component with component-type HelloWorldComponent

我的标签库:

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

关键是,如果我将我的自定义组件放在 faces-config 中,一切都会完美无缺,但作为一个非常顽固的 JSF 新手,我相信必须有办法让注释工作,而无需在 faces-config 中注册自定义组件.我希望有很多自定义组件,所以我想知道如何让它们与纯注释一起工作,以免在 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>

当我取消注释组件标签时,一切正常。但实际上不想那样做,这不是懒惰而是强制计算机做我想做的事;)。

我的项目结构:(我使用 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..  

我已经浏览过的主题: JSF custom component is not found
Cannot find annotated custom JSF2 component in jar
Custom Component using JSF

如果我不必定义我的标签库,那将是理想的,但我想这是不可能的?但首先我想让注释工作......感谢任何帮助

最佳答案

尝试将 my.taglib.xml 文件放入 META-INF 目录。

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

关于java - JSF 2 自定义组件注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759309/

相关文章:

java - JSF 消息 : un-localize?

java - RESTLet 2.0.9 - 有关多个 @Get 和 @Post 注释的查询?

java - Android 6.0 即使拥有权限 777 chmod 也无法读取数据目录中的文件

eclipse - Spring hello world 示例错误

java - Spring - 参数值与预期类型不匹配 [java.lang.Long]

java - Crudrepository - 没有找到依赖项类型的合格 bean : expected at least 1 bean which qualifies as autowire candidate for this dependency

java - 命名约定 JUnit 后缀或前缀 Test

java - 我的应用程序崩溃了java.lang.nullpointerexception

jsf - 从 primefaces 数据表导出的 Excel 文件中不显示标题

java - 具有相对路径的 JSF 导航规则