LoginSignup
1
3

More than 3 years have passed since last update.

JSFのタグがHTMLにならないときの対応方法

Last updated at Posted at 2020-07-21
  • 環境
    • CentOS Linux release 7.8.2003 (Core)
    • Payara Server 5.194
    • Eclipse IDE for Enterprise Java Developers.Version: 2020-03 (4.15.0)
    • openjdk version "11.0.7" 2020-04-14 LTS

事象 : JSFのタグがHTMLにならないでそのまま表示される

JSFのプロジェクトをつくって早速XHTMLを書いたけど・・・JSFのタグがそのまま出力されている
スクリーンショット 2020-07-21 22.53.37.png

index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
    <title>JSFのタグがHTMLにならないときの対応方法</title>
</head>
<body>
  <h3>JSFのタグがHTMLにならないときの対応方法</h3>
  h:selectBooleanCheckboxでチェックボックスを書いてみた
  <div>
    <h:selectBooleanCheckbox id="checkBox" value="false" />
    <h:outputLabel for="checkBox" value="チェックボックス"/>
  </div>
</body>
</html>

原因 : web.xmlでのURL定義と使っているURLがあっていないから

servlet-mappingは、ウェブアプリケーションにアクセスするURLの指定方法を定めます。
わかりやすいJava EE ウェブシステム入門 川場隆 著

WEB-INF/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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>tryJsf</display-name>
...省略...
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
</web-app>

web.xmlの定義だとhttp://xxxxxx/{display-name}/faces/index.xhtmlで表示する
が、今回の事象はhttp://xxxxxx/{display-name}/index.xhtmlで表示したので失敗した。

対応1 : 使うURLをweb.xmlに合わせる

http://xxxxxx/{display-name}/index.xhtmlを使うとちゃんと表示される
スクリーンショット 2020-07-21 22.49.16.png

対応2 : web.xmlを使うURLに合わせる

web.xmlを修正して再ビルドして再実行する

WEB-INF/web.xml
...省略...
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

スクリーンショット 2020-07-21 22.59.39.png

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3