2011-08-13 16 views
0

独自のカスタムタグを作成することを学んでいますが、少し問題があります。このシンプルなアプリケーションで作成したタグを使用できません。私はすべてうまくいったと思うが、私が作成した新しい図書館への道が間違っているのではないかと心配している。多分、誰かが私の誤りがどこにあるのかを見つけ出し、その理由を理解するのを助けることができます。これは私がこれまでにやったことです:独自のカスタムタグ(JSF 2.0)の作成に関する問題

1 - 私はその後、私のように行動するの.xmlファイルを作成したXHTMLチャンク(mybutton.xhtml)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 

<ui:composition> 
    <h:commandButton type="submit" value="#{buttonSubmitLabel}" /> 
    <h:commandButton type="reset" value="#{buttonResetLabel}" /> 
</ui:composition> 
</html> 

2 - としてタグを作成しましたすべての私のカスタムタグがインデックス化されたライブラリ(mytagsconfig.taglib.xml)

<?xml version="1.0"?> 
<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://mytags.com/facelets</namespace> 
    <tag> 
     <tag-name>mybutton</tag-name> 
     <source>mytags/mybutton.xhtml</source> 
    </tag> 
</facelet-taglib> 

3 - 私はweb.xmlに私の新しいライブラリを登録しようとしたので、私はそれを使用することができます

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 
    <display-name>CHAPTER 5 Creating your own Custom tags</display-name> 
    <welcome-file-list> 
     <welcome-file>index.xhtml</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

    <!-- REGISTERING A CUSTOM TAG INTO JSF APPLICATION --> 
    <context-param> 
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name> 
    <param-value>/WEB-INF/mytagsconfig.taglib.xml</param-value> 
</context-param> 
</web-app> 

4 - そして最後には、私は、これは私のフォルダ構造である

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:mytags="http://mytags.com/facelets"> 

<ui:composition template="WEB-INF/templates/masterLayout.xhtml"> 
    <ui:define name="pagetitle"> 
    Defining custom tags 
    </ui:define> 
    <ui:define name="content"> 
    Defining custom tags is a 3 step process: 
    <ul> 
     <li>Use ui:compisition to create some content.(Custom tags are stored in WEB-INF/customtags)</li> 
     <li>Declares the custom tag in a tag library descriptor into the WEB-INF folder(Example: mycustomtags.taglib.xml).</li> 
     <li>Register the tag library descriptor in the web.xml.</li>   
    </ul> 

    <!-- Here should go a call to the new created tag --> 
    <mytags:mybutton buttonSubmitLabel="Submit" buttonResetLabel="Reset" /> 

    </ui:define>  
</ui:composition> 

</html> 

(テンプレートに挿入されているコンポーネントの内部で私の場合は)いくつかのページにタグを使用してみてください:

enter image description here

**** ****アップデート 私がいるindex.xhtmlページが、Cを参照してください構築(私は2つのボタンが表示されません)

+0

私はプログラムを実行し、ブラウザで私は404エラーが表示され、コンソールが言う@BalusC: 'WARNING:StandardWrapperValve [サーブレットを顔]:PWC1406:のServlet.service()サーブレットのサーブレットの例外が投げられました javax.faces.view.facelets.TagAttributeException:/WEB-INF/templates/masterLayout.xhtml @ 33,66 無効パス:WEB-INF/templates/defaultContent.xhtml'テンプレートに何か問題があるようなものですが、何も分かりません。私は上記のテンプレートを貼り付けます。 – sfrj

+0

FYI:上記のエラーは、単にworkspace/project/deployが不正な場合に発生します。それを清掃/再建した後に消された私の削除された回答のコメントに記載されているOP。 – BalusC

答えて

6

あなたのtaglib宣言はweb.xmlで、正しいファイル名を指していません。

あなたは/WEB-INF/mytagsconfig.taglib.xmlを作成しましたが、web.xmlとして/WEB-INF/mytags.taglib.xmlと宣言しました。それに応じてそれを修正します。

問題に直接関係はありませんが、JSF/Facelets 2.0互換のtaglibルート宣言およびweb.xmlコンテキストパラメータ名にアップグレードすることを検討してください。

<?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"> 
    <!-- Taglib config here. --> 
</facelet-taglib> 

<context-param> 
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name> 
    <param-value>/WEB-INF/mytagsconfig.taglib.xml</param-value> 
</context-param> 
+0

あなたが推奨するようにfacelets2.0にアップグレードし、paramnameを 'faces.LIBRARIES'から' faces.FACELETS_LIBRARIES'に変更しました。ブラウザに新しいカスタムタグが表示されます。 Everithingはうまく動作します。ニース今私は自分のタグを作成する方法を知っています:)ありがとうBalusC – sfrj

+0

あなたは大歓迎です。この関連する質問/回答も便利です:http:// stackoverflow。com/questions/6822000/when-to-use-uiinclude-tag-files-composite-components-and-or-custom-component – BalusC

+0

クールな、それは本当に便利なものです:) – sfrj

関連する問題