[Java] How to respond when JSF tags are not HTML

1 minute read

-Environment -CentOS Linux release 7.8.2003 (Core) -Payara Server 5.194 -Eclipse IDE for Enterprise Java Developers.Version: 2020-03 (4.15.0)

Phenomenon: JSF tags are displayed as they are without being HTML

I created a JSF project and immediately wrote XHTML, but the JSF tags are output as is. ![Screenshot 2020-07-21 22.53.37.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/159761/fa8cd781-8633-c6f6-b0d1-(8037f2b1554e.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>Correspondence when JSF tag is not HTML</title>
</head>
<body>
  <h3>Correspondence method when JSF tag is not HTML</h3>
  I tried to write a check box with h:selectBooleanCheckbox
  <div>
    <h:selectBooleanCheckbox id="checkBox" value="false" />
    <h:outputLabel for="checkBox" value="checkbox"/>
  </div>
</body>
</html>

Cause: The URL definition in web.xml does not match the URL you are using

servlet-mapping defines how to specify the URL to access the web application. Introduction to Java EE Web System by Takashi Kawaba

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>
...abridgement...
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
</web-app>

If it is the definition of web.xml, it is displayed by http://xxxxxx/{display-name}/faces/index.xhtml However, this event failed because it was displayed at http://xxxxxx/{display-name}/index.xhtml.

Support 1: Match the URL to be used with web.xml

It is displayed properly when using http://xxxxxx/{display-name}/index.xhtml Screenshot 2020-07-21 22.49.16.png

Support 2: Match the URL using web.xml

Modify web.xml, rebuild and rerun

WEB-INF/web.xml


...abridgement...
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

![Screenshot 2020-07-21 22.59.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/159761/aa3a6667-d66e-1259-b4ea-(337db86de404.png)

Tags: ,

Updated: