はじめに
JSF2.2 (JSR-344 2013年) から Facelet タグライブラリのXMLネームスペースが変更になりました。となりました。Sun 時代のネームスペースは下位互換のために現在でも有効なため、いたるところで java.sun.com が使われ続けておりややこしい感じなのでまとめておきます。
XMLネームスペース
新旧の比較は以下のようになります。
| Library | prefix | JSF2.1まで | JSF2.2以降 |
|---|---|---|---|
| Composite Components | cc |
http://java.sun.com/jsf/composite | http://xmlns.jcp.org/jsf/composite |
| Faces Core | f |
http://java.sun.com/jsf/core | http://xmlns.jcp.org/jsf/core |
| HTML_BASIC | h |
http://java.sun.com/jsf/html | http://xmlns.jcp.org/jsf/html |
| JSTL Core | c |
http://java.sun.com/jsp/jstl/core | http://xmlns.jcp.org/jsp/jstl/core |
| JSTL Functions | fn |
http://java.sun.com/jsp/jstl/functions | http://xmlns.jcp.org/jsp/jstl/functions |
| Facelets Templating | ui |
http://java.sun.com/jsf/facelets | http://xmlns.jcp.org/jsf/facelets |
| Pass Through Attributes | p |
http://java.sun.com/jsf/passthrough | http://xmlns.jcp.org/jsf/passthrough |
| Pass Through Elements | jsf |
http://java.sun.com/jsf | http://xmlns.jcp.org/jsf |
互換性は維持されており、旧URLも依然として使えますが、新しいURLを使うことが推奨されます。
xhtml 上では以下のように定義することになります。
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:cc="http://xmlns.jcp.org/jsf/composite" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:p="http://xmlns.jcp.org/jsf/passthrough" xmlns:jsf="http://xmlns.jcp.org/jsf"> </html>
定義ファイルの所在
IDE はこのURLを利用して定義ファイルを取得しますが、 http://xmlns.jcp.org/jsf.. のURLからはタグライブラリの定義ファイルを取得することができません。旧 URL も現在は無効です。
XMLの仕様上、xmlns の URL は単なる ID であり、リソースを一意に識別するために URL が使われているだけなので、無効な URL が記載されていても仕様上は特に問題ありません。
通常は JSF の実装 JAR の中にあるタグライブラリの定義が自動的に適用されますが、うまくいかない場合は定義ファイルを javax.faces-X.X.X.jar や jakarta.faces-X.X.X.jar などから得ることができます(Jakarta EEへの移管でややこしいですが)。
JAR の中の com\sun\faces\metadata\taglib に定義ファイルが格納されており、対応は以下の通りになります(GitHub上では mojarra/impl/src/main/resources/com/sun/faces/metadata/taglib/ )。
| prefix | url | 定義ファイル |
|---|---|---|
cc |
http://xmlns.jcp.org/jsf/composite | composite.taglib.xml |
f |
http://xmlns.jcp.org/jsf/core | facelets_jsf_core.taglib.xml |
h |
http://xmlns.jcp.org/jsf/html | html_basic.taglib.xml |
c |
http://xmlns.jcp.org/jsp/jstl/core | jstl-core.taglib.xml |
fn |
http://xmlns.jcp.org/jsp/jstl/functions | jstl-fn.taglib.xml |
ui |
http://xmlns.jcp.org/jsf/facelets | ui.taglib.xml |
p |
http://xmlns.jcp.org/jsf/passthrough | facelets_passthrough_attributes.taglib.xml |
jsf |
http://xmlns.jcp.org/jsf | facelets_passthrough_elements.taglib.xml |
Facelet タグライブラリの定義
JSF2.1 までは以下のような定義でした。
<!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:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> ... </html>
JSF2.2 からは以下のように定義することになります。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/composite/html" xmlns:ui="http://xmlns.jcp.org/jsf/composite/facelets"> ... </html>
DOCTYPE は、JSF2.2 の場合はデフォルトで HTML5 の <!DOCTYPE html> となります。 Facelet で <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"・・ のように定義していたとしても、レスポンスの DOCTYPE は <!DOCTYPE html> となります。JSF2.2 からは HTML5 friendly markup がデフォルト値になったということになります。
DOCTYPE の設定
DOCTYPE として旧来の(XHTMLに定義したそのままの)値を出力するには、process-as の定義を
JSF2.2 からは HTML5 friendly markup となっているため、 <!DOCTYPE html> として定義しておくのが良いでしょう。
このデフォルトの挙動を変更するにはfaces-config.xml の process-as で以下のように指定します。
<faces-config-extension> <facelets-processing> <file-extension>.xhtml</file-extension> <process-as>xhtml</process-as> </facelets-processing> </faces-config-extension>
process-as には、html5, xhtml, xml, jspx が指定でき、デフォルト html5 となっています。
process-as の定義による扱いの違いは以下の通りです。
| 項目 | html5 | xhtml | xml | jspx |
|---|---|---|---|---|
| XML Doctype | Simplified to <!DOCTYPE html> |
passed through | consumed | consumed |
| XML declaration | passed through | passed through | consumed | consumed |
| Processing instructions | passed through | passed through | consumed | consumed |
| CDATA section start and end tags | passed through | passed through | consumed | consumed |
| Escaping of inline text | escaped | escaped | escaped | not escaped |
| XML Comments | passed through | passed through | consumed | consumed |
passed through は何もせずにそのまま出力する意となります。
XML Doctype は、 process-as が html5(デフォルト) の場合は <!DOCTYPE html> に単純化されます。 xhtml と定義されていた場合は、そのまま出力となります。なお、Doctype の内容自体は、Facelet の処理内容には何ら影響を与えません。