2012-03-13 125 views
11

我想創建自定義JSF 2.0組件,但無法使其工作。 我的組件的定義如下:未找到JSF自定義組件

@FacesComponent(value = "myCustomComponent") 
public class CommaSeperatedOutput extends UIComponentBase { ... } 

的taglib會是這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<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://www.company.com/tags</namespace> 
<tag> 
    <tag-name>custom</tag-name> 
     <component> 
     <component-type>myCustomComponent</component-type> 
     </component> 
</tag> 
</facelet-taglib> 

我的臉,配置是這樣的:

<faces-config version="2.0" 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"> 
<application> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application> 
</faces-config> 

我收到以下錯誤:

SEVERE: JSF1068: Component with componenttype myCustomComponent could not be instantiated. 
javax.faces.FacesException: Expression Error: Named Object: myCustomComponent not found. 

不確定它是否重要,但我在此使用Spring 3.1和JSF 2.1。所以依賴關係由Spring管理。

任何想法這裏發生了什麼?

解決方案:

好像春天來了壞人。 我已經移除組件註釋@FacesComponent(value = "myCustomComponent")和,而不是像這樣我的臉,配置定義它:

<component> 
    <component-type>myCustomComponent</component-type> 
    <component-class>com.company.jsf.component.CommaSeperatedOutput</component-class> 
</component> 

現在,它的工作原理。

+2

這無疑是一個春天的問題。 JSF部分看起來很好。所以我添加了'[spring]'標籤,這樣你們也可以從Spring人那裏得到關注。 – BalusC 2012-03-13 13:46:41

+1

而不是自定義組件,你也可以嘗試複合組件。 – 2012-03-14 04:22:10

+0

這是一個治療伴侶。 – Makky 2013-04-25 08:50:56

回答

-1

另外,既然你已經配置了faces-config.xml文件中使用Spring容器,你可以使用Spring註解@Component("myCustomComponent")

+0

不,這意味着自定義組件將成爲單身人士。這不是我想要的。 – flash 2012-05-13 11:17:09

+1

按照第二個註釋操作範圍:例如@Scope(「request」)或@Scope(「prototype」)。試試這個備忘單的註釋:http://refcardz.dzone.com/refcardz/spring-annotations – 8bitjunkie 2012-05-14 08:42:07