Java Servlet 4.0のスキーマ定義が覚えられないのでメモ。
公式の定義がどこに載っているのかわからなかった。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
ついでにスキーマ定義の意味もわかっていなかったので調べてみた
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
「名前空間の名前」を定義する。
これでweb.xmlで使っているタグ要素とかがどの名前空間に属しているかが定義される。
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
(一つ宣言を飛ばして)
ここで先に定義した名前空間http://xmlns.jcp.org/xml/ns/javaee
のスキーマ定義ファイル(.xsd)の参照先を指定している。
これは、「<web-app>
とか <servlet>
などのエレメントは、名前空間http://xmlns.jcp.org/xml/ns/javaee
に属していて、その定義内容はhttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
の定義ファイルを参照してね。」って意味と理解しました。
ちなみにweb-app_4_0.xsdが、Java Servlet 4.0を指していると思われる。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation
が属している名前空間を宣言している。
schemaLocation
は、名前空間 の 定義ファイルの場所を参照するために使っている。
まとめ
web.xmlでは2つの名前空間を定義しており、それぞれの用途は以下の通りという解釈。
- エレメント(タグ)の名前空間は、
http://xmlns.jcp.org/xml/ns/javaee
に属している。 - 名前空間の定義ファイルは、
http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
。 - 名前空間と定義ファイルの紐付ける為に
schemaLocation
の名前空間も宣言している