I want to iterate list in paragraph form in JSF page...
for E.g.
I have list and i want to iterate it's value as
like
list<String>={'test1','test2','test3'}
I want JSF output as
test1:..test2:...test3...
how to achieve this ?
Hope this is helpful.
<html xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:repeat var="test" value="#{someClass.methodThatReturnList}">
#{test}:
</ui:repeat>
JSF way is h:dataTable but since you want it just to be displayed in one line you can go with JSTL
<c:forEach items="${yourBean.youeLIst}" var="para">
<c:out value="${para}"/>
</c:forEach>
Related
I have simple jsp page (page.jsp), and a simple java class (classWithArray) with an arraylist (list) in it.
How can I get access to the arraylist from jsp, for example to show it in a table?
Use JSTL's forEach tag to iterate over a collection (an ArrayList in this case in particular) inside a JSP, take a look at this post for more details.
you can iterate over an ArrayList using c:forEach tag from JSTL
Below is the sample code which iterates over an peopleList List.
<c:forEach var="person" items="${people.peopleList}">
<tr>
<td>${person.name}</td>
</tr>
</c:forEach>
use page tag to import a java.util.List in your JSP page use
<%# page import="java.util.List" %>
I have been looking how to implement a thing in struts2 jstl but it is impossible for me to find the way.
When I load the jsp page from the action, I have a list of String lists.
I want to create as divs as elements have the list, but inside every div, I want to create as links as the third element of the sub-list.
So I use the s:iterator tag to parse the list. But I don't know how to iterate "${item[2]}" times inside the first iterator.
The code would be something like this:
<s:iterator value="functions" var="item" status="stat">
<span class="operation">${item[1]}</span>
<div id="${item[0]}">
<s:for var $i=0;$i<${item[2]};$i++>
Link $i
</s:for>
</div>
</s:iterator>
Where I have put the s:for tag is where I would like to iterate "${item[2]}" times...
Anyone can help me?
Thank you very much in advance,
Aleix
Make sure you've got the JSTL core library in scope in your JSP page:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
And simply use <c:forEach>. Something like this:
<c:forEach var="i" begin="0" end="${item[2] - 1}">
Link ${i}
</c:forEach>
You should use List of Map if appropriate, example :
Action class
// List of raw type Map
private List<Map> functions = Lists.newArrayList(); // with getter
#Override
public String execute() {
// loops {
Map map = Maps.newHashMap();
map.put("id", id);
map.put("operation", operation);
map.put("count", count); // count is int/Integer
functions.add(map);
// }
return SUCCESS;
}
.jsp
<s:iterator value="functions">
<span class="operation">${operation}</span>
<div id="${id}">
<s:iterator begin="0" end="count - 1" var="link">
Link ${link}
</s:iterator>
</div>
</s:iterator>
or with <s:a /> (example)
<s:a action="action_name" id="%{link}" anchor="%{link}">Link ${link}</s:a>
output
<a id="[id]" href="/namespace/action#[anchor]">Link [link]</a>
See also
Struts2 Guides -> Tag Reference -> s:iterator
I have this piece of code:
<c:if test="#{utils.getCounterOfCharOccurence(hideTypes, ';') != 0}">
<ui:repeat value="#{document.instanceList}" var="instance">
<c:set var="columnRendered" value="true"></c:set>
<c:forEach items="${hideTypes.split(';')}"
var="hideType">
<h:outputText value="#{hideType eq instance.documentInstanceType.mimeType}"/>
<c:if test="#{hideType eq instance.documentInstanceType.mimeType}">
<c:set var="columnRendered" value="false"></c:set>
<h:outputText value="#{columnRendered}|"/>
</c:if>
</c:forEach>
<a:outputPanel rendered="#{columnRendered == 'true'}">
<up:mimeTypeIcon type="#{instance.documentInstanceType.mimeType}"
icon="#{instance.documentInstanceType.iconPath}"
key="#{instance.instanceKey}" referenced="false"/>
</a:outputPanel>
</ui:repeat>
</c:if>
As you see, i render that outputPanel only when columnRendered is true.
Well, there are situations when this (used only for tests to approve what it should do):
<h:outputText value="#{hideType eq instance.documentInstanceType.mimeType}"/>
is true so it should enter in c:if and switch columnRendered to false. But it doesn't, so columnRendered is true forever...
Do you know why?
JSF and JSTL doesn't run in sync as you'd expect from the coding. JSTL runs during build time of the view (when the JSF component tree is to be populated) and JSF runs during render time of the view component tree (when HTML output is to be generated). You can visualize it as follows: JSTL runs first from top to bottom and then hands over the result to JSF which in turn runs from top to bottom again.
In your particular case, the object instance is never present in JSTL.
Instead of c:forEach, you should use ui:repeat and instead of c:if you should use JSF component's rendered attribute. I'd like to give a rewrite of the code, but the usage of hideTypes is a mess. Rather convert it to a List<String> in the model and it'll be much easier to do with pure JSF. Here's a kickoff example assuming that hideTypes is a List<String>:
<h:panelGroup rendered="#{not empty hideTypes}">
<ui:repeat value="#{document.instanceList}" var="instance">
<a:outputPanel rendered="#{!hideTypes.contains(instance.documentInstanceType.mimeType)}">
<up:mimeTypeIcon type="#{instance.documentInstanceType.mimeType}"
icon="#{instance.documentInstanceType.iconPath}"
key="#{instance.instanceKey}" referenced="false"/>
</a:outputPanel>
</ui:repeat>
<h:panelGroup>
The c:if test always fails for me and it never gets inside the loop. I am using the following namespaces
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:c="http://java.sun.com/jstl/core"
The string ('array') to be split is "Tom and Jerry are GAP1 friends"
<s:decorate template="/layout/display-text.xhtml">
<c:set var="array" value="#{_mybean.value}"/>
<c:set var="space" value="#{fn:split(array, ' ')}"/>
<c:set var="len" value="#{fn:length(space)}"/>
<h:outputText value="total length = #{len}"/><br/>
<c:forEach begin="0" end="5" var="index">
<h:outputText value="index = #{index}, value = #{space[index]}"/><br/>
<c:set var="val" value="#{space[index]}"/>
<c:if test="#{fn:startsWith(val, 'GAP')}">
<h:outputText value="Found keyword parameter GAP" /><br/>
</c:if>
</c:forEach>
</s:decorate>
The JSTL core URI is invalid. As per the JSTL TLD it should be (note the extra /jsp):
xmlns:c="http://java.sun.com/jsp/jstl/core"
That said, mixing JSF with JSTL is never been a good idea. It won't always give results as you'd expect because they doesn't run in sync as you would expect from the coding. It's more that JSP/JSTL runs from top to bottom first and then hands over the produced result to JSF to process further from top to bottom again. That would cause some specific constructs to fail. Better use pure JSF components/attributes instead.
Instead of c:forEach, rather use Seam's a4j:repeat or Facelets' ui:repeat and instead of c:if make use of the rendered attribute of the JSF component which has to be toggled to show/hide. Instead of all that JSTL c:set, write appropriate code logic in managed bean constructor or action method or getter.
The JSTL functions (fn) taglib is however still highly valuable in JSF. You can keep using it.
Inside a nested foreach, accessing the same variable is returning different values. This happens when the page is reloaded, not on first load.
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
(...)
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
(...)
<c:forEach items="#{controller.availableTransitions}" var="transition">
<c:forEach items="#{transition.availableTransitions}" var="transitionItem">
<h:outputText value="1_#{transitionItem.name} 2_#{transitionItem.name}" />
3_#{transitionItem.name} 4_#{transitionItem.name}
</c:forEach>
</c:forEach>
</ui:composition>
After page reload, transitionItem.Name returns the correct value for 3 and 4, and different values for 1 and 2. Maybe a JSF-JSTL integration problem?
I see that you are using Facelets.
Maybe you can try to replace your <c:forEach> by <ui:repeat>...
The code will then become:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
(...)
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
(...)
<ui:repeat value="#{controller.availableTransitions}" var="transition">
<ui:repeat value="#{transition.availableTransitions}" var="transitionItem">
<h:outputText value="1_#{transitionItem.name} 2_#{transitionItem.name}" />
3_#{transitionItem.name} 4_#{transitionItem.name}
</ui:repeat>
</ui:repeat>
</ui:composition>
In general, I try to use ui:repeat most of the time. When I was having c:set issues, I found this blog, which was very helpful and may apply in your case also.
https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat
Found a workaround, by getting rid of the inner forEach loop, thus returning a linear list from the controller.