Passing dynamic value as a key to Map in JSF 2.0 - jsf-2

I have a List of keys say 'ListA'. And a map of keys & list say 'MapA'. I need to iterate the 'ListA' & for every key need to get its value from 'MapA'. And those values serve as the model for dataTable.
For this purpose,I'm using h:datatable inside ui:repeat.
<ui:repeat var="entry" value="#{bean.sampleDTO.sampleList}"
varStatus="row">
<tr>
<td>#{entry.key}</td>
<td><h:datatable value="#{bean.map[#{entry.key}]}" var="row">
<h:column>
// something
</h:column>
</h:datatable></td>
</tr>
</ui:repeat>
Please consider the value of datatable:
value="#{bean.map[#{entry.key}]}"
The issue is that the key is a variable which I get from #{entry.key}. #{bean.map[#{entry.key}]} is an invalid EL expression as 2 # can't be used.
Thanks,
Tarun Madaan

for the el expression : try this
value="#{bean.map[entry.key]}"
you dont need to use #{} inside #{}

Related

Does not work properly id generation on ui:repeat My faces 2.2.9 + prime faces 5.3

Actually I'm working with Myfaces version 2.2.9 and I've the following structure to generate any panel according with a specific number selected by the user.
...
<ui:repeat value="#{garajes}" var="garaje" varStatus="loop">
<p:panelGrid >
<h:outputLabel value="Numero de garaje #{loop.index+1}: " />
<h:outputLabel value="Matricula #{loop.index+1}: " />
<p:inputText value="#{garaje.numeroGaraje}" maxlength="5" >
</p:inputText>
<p:inputText id="matriculaInmobiliariaGaraje-#{loop.index+1}" value="#{garaje.matriculaInmobiliaria}"
maxlength="20">
</p:inputText>
...
</p:panelGrid>
</ui:repeat>
....
So, when is rendered the above code the identifiers are weird, has another things like the following image:
So I don't know how to remove this weird things inside of id
Note: I need a specific id to update another component inside of the loop.
What can I do to get a right identifiers inside of ui:repeat?
As to the concrete problem, just give all NamingContainer components a fixed ID. This includes the <ui:repeat> itself.
<ui:repeat id="garajes" ...>
As to the concrete requirement, you're overcomplicating things. A NamingContainer will all by itself worry about uniqueness of IDs of children. Your attempt in id="matriculaInmobiliariaGaraje-#{loop.index+1}" won't work at all as #{loop} variable isn't available during view build time, when the component is being instantiated with id. Get rid of it. You can inside a NamingContainer just use a relative ID to reference another component in the same NamingContainer.
<ui:repeat ...>
<p:inputText id="foo" />
<p:inputText ...><p:ajax ... update="foo" /></p:inputText>
</ui:repeat>
This will work just fine.
See also:
How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
How to use EL with <ui:repeat var> in id attribute of a JSF component

Is there a way to get the index of each item in a list being passed to JSTL c:forEach

If I use the JSTL forEach loop to display a list of all returned items from my controller, Is there a way to output an index number for each item that is returned in the list?
<c:forEach var="item" items="#{Controller.allItems}" >
<tr><td>{index number here???} : #{item.name}</td></tr>
</c:forEach>
That's what the attribute varStatus is for:
<c:forEach var="item" items="#{Controller.allItems}" varStatus="status" >
<tr><td>${status.index} : #{item.name}</td></tr>
</c:forEach>
VarStatus contains many other Attributes as well:
begin
end
index
step
even
odd
first
last

How to use a map key in EL which is a concatenation of static string and EL variable [duplicate]

This question already has answers here:
String concatenation in EL for dynamic ResourceBundle key
(4 answers)
Closed 7 years ago.
I have a <p:dataTable var="object"> with a <p:commandLink> inside. In my bean I have a Map whose value I want to display in <p:commandLink value> . I need to pass a key to this map which is a concatenation of "someString" and #{object.firstName}. I would something like this to work:
value="#{bean.map['someString'+object.firstName]}"
How can I achieve this?
You can concatenate strings in EL by creating a new EL variable with the string and EL expression just inlined. You can use <c:set> for that.
<c:set var="key" value="someString#{object.firstName}" />
<p:commandLink value="#{bean.map[key]}" ... />
Alternatively, if you're on EL 2.2 already which supports invoking direct methods, then you can just directly make use of String#concat() method.
<p:commandLink value="#{bean.map['someString'.concat(object.firstName)]}" ... />
If you're not on EL 2.2 yet, but are using EL 2.1, then you can always install JBoss EL to have the same feature.
Mr AC_1985 didn't respond but for information I have some working code along these lines.
<h:panelGrid columns="1" cellpadding="5" style="width:100%">
<p:inputTextarea id="factsText" style="width:100%; height:100%;" rows="18" cols="100"
value="#{property.model.property.facts[property.model.factsLanguage.code]}"
counter="display" maxlength="2000" counterTemplate="{0} characters remaining." autoResize="false" />
<h:outputText id="display" />
</h:panelGrid>
Here I have a combo box where I can select a language. Then the text is retrieved from the Map for the language selected.
Thanks My BalusC for your invaluable help, as always.
Allan

Counters in Loops in Thymeleaf

Is there a way to do a loop in Thymeleaf without a list?
I'd like to essentially convert this snippet to Thymeleaf:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
<form:option value="${year-i}" />
</c:forEach>
</form:select>
-- Update --
I've decided this is along the lines of how I want to do it, but I'm not sure about the springEL syntax:
<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
In case you are still looking for the correct SpEL syntax,
here's what worked for me:
<option th:each="i : ${#numbers.sequence( 1, 100)}"
th:value="${ (new org.joda.time.DateTime()).getYear() - i }"
th:text="${ (new org.joda.time.DateTime()).getYear() - i }">1</option>
Notice:
added th:text to set the option text.
used Joda-Time instead as java.util.Date wouldn't give me the desired outcome
Read this discussion on java.util.Date and getYear()
You can use the special thymleaf iteration variable inside the each block.
This special variable name is the name of your element variable concatenate with the keyword 'Stat' (ex: elt -> eltStat)
This variable gives you many information related to the iteration.
You can also specify this variable name after your element variable. For example:
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
More information in the official documentation below:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

Wrap the text in dataList of primefaces

<p:dataList value="#{testBean.students}" var="stud" rows="5" >
<p:column>
<p:commandLink value="#{stud.name}" action="#{testBean.edit}" process="#this">
</p:commandLink>
</p:column>
</p:dataList>
I am using simple data list and used command Link to edit.This data list is a list of students.
My question is datlist has student name with
"teststudent1234567890123456789012345678901234567890".
How do i wrap this name in a datalist so that I can display only teststudent and wrap word so that it can fit in datalist. Validation is done and student name can have a maximum of 50 character.
Use #{fn:substring(name, begin, end)} like that #{fn:substring(stud.name, 0, 10)}. Don't forget to include the namespace in your view in order to make jstl functions available for your page: xmlns:fn="http://java.sun.com/jsp/jstl/functions". Here you have an example and also the specification of jstl 1.1 functions.

Resources