[JAVA] How to specify id attribute in JSF

--Environment - CentOS Linux release 7.8.2003 (Core) - Eclipse IDE for Enterprise Java Developers.Version: 2020-03 (4.15.0) - openjdk version "11.0.7" 2020-04-14 LTS - JSF 2.3.9

In JSF, the id attribute value may be output as a value different from the value that was set in xhtml. The book described the name attribute, but the id attribute may also be able to understand the correspondence with the component tree.

FacesServlet generates an HTML file from the component tree and sends it to the browser. Such a transformation is called rendering. Even after rendering to an HTML file, the HTML tags bound to the backing bean are embedded with the name attribute as shown below so that you can see the correspondence with the internal component tree. Number: \ \
Introduction to Easy-to-understand Java EE Web System-Shuwa System

Problem 1: The id attribute does not have the specified value inside the h: form tag

Outside the h: form tag, the id attribute value is as specified.

If ** outOfForm ** is specified for the id attribute value of h: inputText

<h:inputText id="outOfForm" value="Outside the form tag, the id will be as specified" />

The id attribute value of ʻinput` is ** outOfForm **.

output


<input id="outOfForm" type="text" name="outOfForm" value="Outside the form tag, the id will be as specified">

Inside the h: form tag, it becomes "form tag id attribute value: specified id attribute value"

If the id attribute is not specified in the h: form tag, it becomes "automatic attribute value: specified id attribute value".

If you specify ** inOfForm ** for the id attribute value of h: inputText without specifying the id attribute of the h: form tag

<h:form>
  <h:inputText id="inOfForm" value="h:If you do not specify the id attribute in the form tag, "automatic attribute value":It becomes "specified id attribute value"" />
</h:form>

The id attribute value of ʻinput` is ** j_idt5: inOfForm **.

output


<form id="j_idt5" name="j_idt5" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="j_idt5" value="j_idt5">
  <input id="j_idt5:inOfForm" type="text" name="j_idt5:inOfForm" value="h:If you do not specify the id attribute in the form tag, "automatic attribute value":It becomes "specified id attribute value"">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-6757148234097573315:8183558224686661226" autocomplete="off">
</form>

If id attribute is specified in h: form tag, it becomes "id attribute value of form tag: specified id attribute value".

If you specify ** formId ** for the id attribute value of the h: form tag and ** inOfForm ** for the id attribute value of h: inputText

<h:form id="formId">
  <h:inputText id="inOfForm" value="h:If you specify the id attribute in the form tag, "id attribute value of the form tag":It becomes "specified id attribute value"" />
</h:form>

The id attribute value of ʻinput` is ** formId: inOfForm **.

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input id="formId:inOfForm" type="text" name="formId:inOfForm" value="h:If you specify the id attribute in the form tag, "id attribute value of the form tag":It becomes "specified id attribute value"">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="1982632179815695771:1244710352148896782" autocomplete="off">
</form>

Solution 1: If prepareId = "false" is specified in the h: form tag, the id attribute value will be the specified value.

prependId Flag indicating whether or not this form should prepend its id to its descendent's id during the clientId generation process. If this flag is not set, the default value is true. JSF 2.2 View Declaration Language: Facelets Variant

A flag that indicates whether the form ID is prepended to its offspring ID during id generation. The default value is true. If you specify ** formId ** for the id attribute value of the h: form tag and ** inOfForm ** for the id attribute value of h: inputText

<h:form id="formId" prependId="false">
  <h:inputText id="inOfForm" value="h:prependId in form tag=If false is specified, the id attribute will be the specified value." />
</h:form>

The id attribute value of ʻinput` is ** inOfForm **.

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input id="inOfForm" type="text" name="inOfForm" value="h:prependId in form tag=If false is specified, the id attribute will be the specified value.">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-3193809963809428842:-8198161006116065679" autocomplete="off">
</form>

Problem 2: The id attribute value does not become the specified value inside the ui: repeat tag

It becomes "automatic attribute value: index number: specified id attribute value" both outside and inside the h: form tag.

If ** inOfRepeat ** is specified for the id attribute value of h: inputText

<ui:repeat var="item" varStatus="stat" value="#{idBean.array}">
  <h:inputText id="inOfRepeat#{stat.index}" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value" />
</ui:repeat>

The id attribute value of ʻinput` is ** j_idt5: {index number}: inOfRepeat **.

output


<input id="j_idt5:0:inOfRepeat" type="text" name="j_idt5:0:inOfRepeat">
<input id="j_idt5:1:inOfRepeat" type="text" name="j_idt5:1:inOfRepeat">

If the id attribute is not specified in the h: form tag, it will be "automatic attribute value: automatic attribute value: index number: specified id attribute value".

If you specify ** inOfRepeat {index number} ** in the id attribute value of h: inputText without specifying the id attribute of the h: form tag

<h:form>
  <ui:repeat var="item" varStatus="stat" value="#{idBean.array}">
    <h:inputText id="inOfRepeat#{stat.index}" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value" />
  </ui:repeat>
</h:form>

The id attribute value of ʻinput` is ** j_idt5: j_idt9: {index number}: inOfRepeat **.

output


<form id="j_idt5" name="j_idt5" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="j_idt5" value="j_idt5">
  <input id="j_idt5:j_idt9:0:inOfRepeat" type="text" name="j_idt5:j_idt9:0:inOfRepeat">
  <input id="j_idt5:j_idt9:1:inOfRepeat" type="text" name="j_idt5:j_idt9:1:inOfRepeat">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="8291831575234858658:4144786523139149206" autocomplete="off">
</form>

When id attribute is specified in h: form tag, it becomes "id attribute value of form tag: automatic attribute value: index number: specified id attribute value".

If you specify ** formId ** for the id attribute value of the h: form tag and ** inOfRepeat {index number} ** for the id attribute value of h: inputText

<h:form id="formId">
  <ui:repeat var="item" varStatus="stat" value="#{idBean.array}">
    <h:inputText id="inOfRepeat#{stat.index}" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value" />
  </ui:repeat>
</h:form>

The id attribute value of ʻinput` is ** formId: j_idt9: {index number}: inOfRepeat **.

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input id="formId:j_idt9:0:inOfRepeat" type="text" name="formId:j_idt9:0:inOfRepeat">
  <input id="formId:j_idt9:1:inOfRepeat" type="text" name="formId:j_idt9:1:inOfRepeat">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-9123594053057548780:-7376239985257011779" autocomplete="off">
</form>

Even if prepareId = "false" is specified in the h: form tag, the id attribute value does not become the specified value.

Even if you specify prependId =" false " in the h: form tag

<h:form id="formId" prependId="false">
  <ui:repeat var="item" varStatus="stat" value="#{idBean.array}">
    <h:inputText id="inOfRepeat#{stat.index}" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value" />
  </ui:repeat>
</h:form>

The id attribute value of the form tag just doesn't stick.

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input id="j_idt9:0:inOfRepeat" type="text" name="j_idt9:0:inOfRepeat">
  <input id="j_idt9:1:inOfRepeat" type="text" name="j_idt9:1:inOfRepeat">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-915378070197893309:-5208595187307997788" autocomplete="off">
</form>

Solution 1: Stop jsf tag and write with html tag

Note: It's a shame if you don't write the id attribute value so that it is unique. It's a very delicate measure. If ** inOfRepeat {index number} ** is specified for the id attribute value of ʻinput`

<h:form id="formId">
  <ui:repeat var="item" varStatus="stat" value="#{idBean.array}">
    <input tyep="text" id="inOfRepeat#{stat.index}" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value" />
  </ui:repeat>
</h:form>

The id attribute value of ʻinputis ** inOfRepeat {index number} **. Since it is out of the JSF world, the id attribute value of theh: form tag does not affect even if there is no prepareId = "false" `.

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input tyep="text" id="inOfRepeat0" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value">
  <input tyep="text" id="inOfRepeat1" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-3641841499499477715:2017944983286472561" autocomplete="off">
</form>

Solution 2: Stop the ui: repeat tag and use the c: forEach tag

Note: It's a shame if you don't write the id attribute value so that it is unique. Since the JSTL Core tag (c :) is a tag inherited from JSP, is it not JSF-like?

If you specify ** formId ** for the id attribute value of the h: form tag and ** inOfRepeat {index number} ** for the id attribute value of h: inputText

<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"
  xmlns:c="http://java.sun.com/jsp/jstl/core"> <!--<<<Postscript-->
<h:form id="formId">
  <c:forEach var="item" varStatus="stat" items="#{idBean.array}">
   <h:inputText id="inOfRepeat#{stat.index}" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value"/>
  </c:forEach>
</h:form>

The id attribute value of ʻinput` is ** formId: inOfRepeat {index number} **.

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input id="formId:inOfRepeat0" type="text" name="formId:inOfRepeat0" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value">
  <input id="formId:inOfRepeat1" type="text" name="formId:inOfRepeat1" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="7276136016284590450:-4018393601695065919" autocomplete="off">
</form>

If you do not want to attach the id attribute value of the h: form tag, specifyprepareId = "false".

<h:form id="formId" prependId="false">
...Same as before...

output


<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <input id="inOfRepeat0" type="text" name="inOfRepeat0" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value">
  <input id="inOfRepeat1" type="text" name="inOfRepeat1" value="h:Inside the form tag ui:If you use the repeat tag, the id attribute value will not be the specified value">
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-4294574413761401134:-7748270303136609237" autocomplete="off">
</form>

Recommended Posts

How to specify id attribute in JSF
How to generate / verify ID token in Java Memo
How to specify the resource path in HTML import
How to check JSF version
How to specify character code and line feed code in JAXB
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to master programming in 3 months
How to get parameters in Spark
How to use InjectorHolder in OpenAM
How to introduce jQuery in Rails 6
How to name variables in Java
How to set Lombok in Eclipse
How to concatenate strings in java
How to install Swiper in Rails
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to output a list of strings in JSF as comma-separated strings
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to do base conversion in Java
[Swift] How to fix Label in UIPickerView
How to have params in link_to method
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to fix system date in JUnit
How to implement coding conventions in Java
How to embed Janus Graph in Java
[rails] How to configure routing in resources
How to map tsrange type in Hibernate
How to implement ranking functionality in Rails
How to implement asynchronous processing in Outsystems
Understand in 5 minutes !! How to use Docker
How to overwrite Firebase data in Swift
How to use credentials.yml.enc introduced in Rails 5.2
How to assemble JSON directly in Jackson
How to execute multiple commands in docker-compose.yml
[For beginners] How to debug in Eclipse
Add SameSite attribute to cookie in Java
How to use ExpandableListView in Android Studio
How to display error messages in Japanese
Summary of how to select elements in Selenium
How to get keycloak credentials in interceptor class
[JavaFX] How to write Eclipse permissions in build.gradle
(Memo) How to solve dummy output in Ubuntu 20.04
How to check the logs in the Docker container