【问题标题】:JSF 2 Custom component annotation does not workJSF 2 自定义组件注释不起作用
【发布时间】:2013-05-21 11:48:04
【问题描述】:

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

@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

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

【问题讨论】:

  • 我不做Maven,但是组件类文件必须在/WEB-INF/classes或/WEB-INF/lib的JAR文件中(包含/META-INF/faces-config.xml)才能让@FacesComponent工作.仔细检查 Maven 生成的 WAR 文件并在必要时更改构建配置。
  • 我的组件类 (HelloWorldComponent) 与我项目中的普通类一起在 WEB-INF/classes 中,我被困住了,因为我已经尝试了我可以在网络上找到的所有提示/解决方案跨度>

标签: java spring jsf annotations


【解决方案1】:

您是否尝试过重新启动应用程序服务器?我不确定 Spring 的情况如何,但在 JBoss 中为我解决了类似的问题。服务器启动时可能会自动注册组件。如果不重新启动,它们必须列在某个文件中。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2017-12-21
      • 2021-03-28
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多