how to use if loop inside if loop using jstl - spring

I have a requirement where if status is 0 then add a css class active else add a css class active1 and for that I have done like the follow
<c:forEach items="${parentList}" var="test">
<c:choose>
<c:when test="${test[1] eq 0}">
<li><a href="#" class="active" value="${test[0].id}-${test[0].category}" id="${parent.id}" onclick="return getQuestions(this);" >
${test[0].name }</a></li>
</c:when>
<c:otherwise>
<li><a href="#" class="active1" value="${test[0].id}-${test[0].category}" id="${parent.id}" onclick="return getQuestions(this);" >
<img src="${pageContext.request.contextPath}/resources/ui_resources/img/checked.jpg" alt="" height="20" width="20"/>${test[0].name }</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
Now my requirement if
${test[0].category} is client then add a button <button name="delete/>
so now 4 condtions arrise
if status is 0 and category is not client then add class active
if status is 0 and category is client then add class ="active" and
add button.
if status is not 0 category is client then add class="active1" and
add button
if status is not 0 and category is not client then only add
class="active".
So can any body please tell me how to use if loop inside if loop using jstl

Instead of a multitude of <c:choose /> use <c:if /> together with a <c:var /> to determine the class to use. You can also use a <:if /> for the button, this will require you to move the rendering of the image to the css.
The JSP would look something like this (from the top of my head).
<c:forEach items="${parentList}" var="test">
<c:set var="clazz" value="active" />
<c:if test="${test[1] ne 0 and test[0].category ne 'client'}">
<c:set var="clazz" value="active1" />
</c:if>
<li>
<a href="#" class="${clazz}" value="${test[0].id}-${test[0].category}" id="${parent.id}" onclick="return getQuestions(this);" > ${test[0].name }</a>
<c:if test="${test[0].category eq 'client'}"><button name="delete" /></c:if>
</li>
</c:forEach>
You would need something like the following to your css class active1
a.active1 {
background-image: url('/resources/ui_resources/img/checked.jpg');
background-repeat: no-repeat;
padding-left: 25px; /* width of the image plus a little extra padding */
display: block;
}

Based on your code block, how just add c:if tag after <c:choose> block?
<c:forEach ...>
<c:choose ...>
</c:choose>
<%-- here to check buuton add or not --%>
<c:if test="${test[0].category == 'client'}"><button name="delete" /></c:if>
</c:forEach

Related

Need to Combine Spring Binding Errors and Custom Exceptions into Same DIV

I need to display SpringMVC's Form errors, as well as any custom "Exception" messages that I store in the request, in the same DIV on my page.
Right now they're in separate DIV's:
<!-- Validation Errors -->
<spring:hasBindErrors htmlEscape="true" name="model">
<c:if test="${errors.errorCount gt 0}">
<div class="errors_div clsErrorTblBorder">
<c:forEach items="${errors.allErrors}" var="error">
<div class="errors clsWhiteBack blue">
<myForm:message messageLinkClass="errorLink"
error="${error}" />
</div>
</c:forEach>
</div>
</c:if>
</spring:hasBindErrors>
<!-- Exceptions -->
<c:if test="${fn:length(requestScope.exceptions) > 0}">
<div class="errors_div clsMsgTblBorder">
<c:forEach items="${requestScope.exceptions}" var="exception">
<div class="messages clsWhiteBack">
${exception}
</div>
</c:forEach>
</div>
</c:if>
The shared condition for an Error DIV should be,
${fn:length(requestScope.exceptions) || errors.errorCount gt 0}
But it's impossible to test this condition, because:
You don't get the errors var. until you do <spring:hasBindErrors>
Anything inside <spring:hasBindErrors> only applies if you have
binding errors. If you don't (e.g. if you only have a custom
Exception message) its logic won't execute, so you can't move the
custom "exceptions" check inside it.
I got some clues from this thread: How to access Spring 3 MVC validator results in JSP without using form taglib , and here's the working result:
<c:if test="${fn:length(requestScope.exceptions) > 0 ||
requestScope['org.springframework.validation.BindingResult.model'].hasFieldErrors()}">
The 2nd part of this condition checks for Binding Errors even prior to the tag Spring:hasBindErrors.

How to create image attributes in HippoCMS 7.9?

I'm currently working on a hippo component within (7.9) and I need the image link, alt, and title:
Component Method:
public HippoGalleryImageSetBean getImage() {
return getLinkedBean(Constants.NS_FLD_IMAGE,
HippoGalleryImageSetBean.class);
}
I would like to write my Component JSP like this below:
<c:forEach var="item" items="${ document.links }"
varStatus="loopStatus">
<hst:link var="image"hippobean="${ item.image.original }" />
<li><img
src="${ image }"
alt="${ image.alt }"
title="${ image.title} ">
</li>
</c:forEach>
The 'alt' and 'title' are defined on the imageset itself and not in the image variants. ${image} in your case is the link generated to an image and not the image object itself.
Try it with:
<c:forEach var="item"
items="${ document.links }"
varStatus="loopStatus">
<hst:link var="imagelink"hippobean="${ item.image.original }" />
<li><img src="${ imagelink }"
alt="${ item.image.alt }"
title="${ item.image.title} ">
</li>
</c:forEach>

jsp forEach Loop through Array of Images and place theme in specific divs

I have 60 images and I want to place 3 images in a div each.
<c:forEach items="${images}" var="image" varStatus="imgCount">
<c:if test="${imgCount.count == 3 }">
<div class="${gridImage.displayposition}">
<img src="${imagePath}${gridImage.image}" />
</div>
</c:if>
</c:forEach>
Would this be the way to accomplish this?
The condition in your c:if will only be true once: when you're on the third item. You can use the mod or % operators in EL to handle the images in groups of three. Take care with the varStatus.count property - it's one-based rather than zero-based, so you'd access the next image in the array with ${images[imgCount.count]}.
<c:forEach items="${images}" var="image" varStatus="imgCount">
<c:if test="${(imgCount.count-1) mod 3 eq 0}">
<div class="${gridImage.displayposition}">
<img src="${imagePath}${image}/>
<img src="${imagePath}${images[imgCount.count]}/>
<img src="${imagePath}${images[imgCount.count+1]}/>
</div>
</c:if>
</c:forEach>
You could also use the step attribute of the forEach tag and eliminate the need for the if statement. Here, you only create a div every third item. The count, however, will still continue in the sequence of 1,2,3... (as opposed to 1,4,7...) so you need to multiply the index by three.
<c:forEach items="${images}" varStatus="imgCount" step="3">
<div class="${gridImage.displayposition}">
<img src="${imagePath}${images[3*(imgCount.count-1)]}"/>
<img src="${imagePath}${images[3*(imgCount.count-1) + 1]}"/>
<img src="${imagePath}${images[3*(imgCount.count-1) + 2]}"/>
</div>
</c:forEach>

JSF 2.2 Table conditional rendering not working

Im trying to display a simple table with an #Entity infos using the following tag-libs:
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
Here's the mapped entity attribute, a boolean:
#NotNull
#Column(name = "ACTIVE", columnDefinition = "BIT", length = 1)
private boolean active;
//... getters and setters
public boolean getActive() {
return active;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active= active;
}
ps.: by debugging the backing bean, im getting the desired list.
Here's my .xhtml (jsf page) snippet:
<ui:repeat var="u" value="#{usersList}">
<tr>
<td>#{u.name}</td>
<td>#{u.login}</td>
<td>#{u.email}</td>
<td>
<c:choose>
<c:when test="${u.active}">
<span style="color: green" class="glyphicon glyphicon-ok"></span>
</c:when>
<c:otherwise>
<span style="color: red"
class="glyphicon glyphicon-remove"></span>
</c:otherwise>
</c:choose>
</td>
</tr>
</c:when>
</ui:repeat>
Altough the list is not empty, the empty message is being shown, and no row is displayed.
If i force the row displaying, removing the conditional rendering, i get the following error:
java.lang.IllegalArgumentException: Cannot convert 1 of type class java.lang.Long to class java.lang.Boolean
for the property active.
I've tried a few solutions before asking, like these:
How does Java expression language resolve boolean attributes? (in JSF 1.2)
javax.el.PropertyNotFoundException when trying to resolve Boolean properties in EL
Update 1: reviewing my code, i realised i was empty testing an object, not a list, updating my snippet also, so, the conditional on the rows displaying its ok now, but the active property issue, still throwing the error.
The problem is that JSF tags (e.g. ui:repeat) are evaluated in a different (later) phase then JSTL tags (e.g. c:when). Therefore the values of your user list are just not available when the c:when statement is evaluated. Sometimes this results in a weird behaviour so that it looks like the values are available. Have a look at this answer to get some details.
You should use the rendered attribute of some JSF component:
<c:choose>
<c:when test="${u.active}">
<span style="color: green" class="glyphicon glyphicon-ok"></span>
</c:when>
<c:otherwise>
<span style="color: red" class="glyphicon glyphicon-remove"></span>
</c:otherwise>
</c:choose>
turns into:
<h:panelGroup rendered="#{u.active}">
<span style="color: green" class="glyphicon glyphicon-ok"></span>
</h:panelGroup>
<h:panelGroup rendered="#{not u.active}">
<span style="color: red" class="glyphicon glyphicon-remove"></span>
</h:panelGroup>
You can also use it for values like this:
<h:outputText rendered="#{u.active}" value="#{u.name}" />
See also:
c:choose not working in JSF
JSTL in JSF2 Facelets… makes sense?

I want to use inline validation function in jsp

hear is the code
<spring:form modelAttribute="commonBean" action="saveStock.htm">
<div class="content" align="center">
<div class="box"><br>
<h1>
<spring:label path=""> Stock Entry</spring:label>
</h1><br><hr>
<table>
<tr>
<th align='left'><spring:label path="">Item Name: </spring:label>
</th>
<td colspan='5'><spring:input path="" ></spring:input>
<span style="font-style: italic;color: red;">
<spring:errors path="" id="iname" onkeyup="iname()">
<img src="green.png" class="def" id="iname_true"/>
<img src="cross.png" class="def" id="iname_false"/>
</spring:errors></span>
</td>
</tr>
and its giving error like
HTTP ERROR: 500
/WEB-INF/jsp/corefiles/stock.jsp(19,22) PWC6239: According to TLD, tag spring:input must be empty, but is not
Plz some one suggest me how to achive that
Change:
<spring:input path="" ></spring:input>
to:
<spring:input path="CommonBeanPropertyName" />
You have to set a path, so that Spring will be able to bind the input field to the corresponding bean property.
Have a look to the documentation .

Resources