How to link label with an input field in data table?

I am curious if there is a nice way to link a label to input field in a data table directly in JSP? Data filling the table are dynamically bounded.

Sample, simplified code below. Values of "label" property are unique in the collection binded to dataTable.

<?xml version="1.0" encoding="UTF-8"?>

<jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:jsp="http://java.sun.com/JSP/Page">

<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" />

<f:view>

<html>

<body>

<h:dataTable value="#{screen$monitoring$errorlog$CorrectHopper.dataset}" var="DATA_ROW">

<h:column>

<f:facet name="header">

<h:outputText value="Name" />

</f:facet>

<h:outputLabel value="#{DATA_ROW.label}" for="#{DATA_ROW.label}_id" />

</h:column>

<h:column>

<f:facet name="header">

<h:outputText value="Value" />

</f:facet>

<h:inputText value="#{DATA_ROW.label}" id="#{DATA_ROW.label}_id" />

</h:column>

</h:dataTable>

</body>

</html>

</f:view>

</jsp:root>

returns:

17:39:01,390 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception

java.lang.IllegalArgumentException: #{DATA_ROW.label}_id

what is related to EL expression in h:inputText's "id" attribute.

Is there any other way to bind label with dynamically created input field in JSP? Do I really have to bind input field directly to component objects with "binding" and set IDs manually with setId() (what is ugly solution because it moves View related logic to Model layer)?

[1828 byte] By [MichalGlombaa] at [2007-11-26 14:08:15]
# 1
Why do you want to set ID's using EL? Those have to be just plain text.
BalusCa at 2007-7-8 1:54:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I've thought of using EL because I have to somehow link a label to an input field dynamically. And I believe it definitely looks better to define such relationships between visual components in JSP, not a backing bean. Is there any other, not-EL, way?

What is the special reason to limit the binding option of UIComponent's "id" attribute to just pure value and to exclude EL?

MichalGlombaa at 2007-7-8 1:54:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> I've thought of using EL because I have to somehow

> link a label to an input field dynamically.

Somehow? Dynamically? Just use plain text :)

> What is the special reason to limit the binding

> option of UIComponent's "id" attribute to just pure

> value and to exclude EL?

With EL you cannot guarantee that the ID's should be and stay unique. JSF requires static and guaranteed unique ID's, because those are used to keep the UIViewRoot ordered and alive.

BalusCa at 2007-7-8 1:54:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> > I've thought of using EL because I have to somehow

> > link a label to an input field dynamically.

>

> Somehow? Dynamically? Just use plain text :)

Well... just look at my code snippet (notice: we want to join the pairs of labels and text boxes placed *in a datatable* [or any other repeater-like component rendering properties of objects from a collection]):

<h:dataTable value="#{screen$monitoring$errorlog$CorrectHopper.dataset}" var="DATA_ROW">

<h:column>

<f:facet name="header">

<h:outputText value="Name" />

</f:facet>

<h:outputLabel value="#{DATA_ROW.label}" for="#{DATA_ROW.label}_id" />

</h:column>

<h:column>

<f:facet name="header">

<h:outputText value="Value" />

</f:facet>

<h:inputText value="#{DATA_ROW.label}" id="#{DATA_ROW.label}_id" />

</h:column>

</h:dataTable>

and what is your idea of using plain text as "id" attribute value in this case? :)

Regards,

Michal

MichalGlombaa at 2007-7-8 1:54:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Just do something like<h:outputLabel value="#{DATA_ROW.label}" for="label" />

<h:inputText value="#{DATA_ROW.label}" id="label" />

Note: JSF will automatically prefix the ID values. The result will be something like "formId:tableId:rowNumber:label".

BalusCa at 2007-7-8 1:54:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...