How to compare 2 values using jstl - spring

I have lists in controller
List l=serviceClientServiceImpl.getSavedParents(clientId);
uiModel.addAttribute("savedParents",l);
List<Parent> parentList =
(List<Parent>) serviceClientServiceImpl.getParents("Parent",Long.valueOf(clientId));
uiModel.addAttribute("parentList", parentList);
and I am retrieving it in jsp like the follow
<ul style="max-width: 300px;" class="nav nav-pills nav-stacked" id="menuId">
<c:forEach items="${parentList}" var="test">
<li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
<input type="checkbox" value="${test.id}">${test.name }</a></li>
</c:forEach>
</ul>
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
<c:forEach items="${savedParents}" var="test1">
<ul style="max-width: 300px;" class="nav nav-pills nav-stacked">
<li class="active"><a href="#" value="${test1.id}" onclick="return getQuestions(this);">
<input type="checkbox" value="${test1.id}"></a></li>
</ul>
</c:forEach>
Now I want to merge the the both and where ever test.id equals to test1.id then I want to add an html class="active"
I tried the following way
<ul style="max-width: 300px;" class="nav nav-pills nav-stacked" id="menuId">
<c:forEach items="${parentList}" var="test">
<c:forEach items="${savedParents}" var="test1">
<c:when test3="${test.id}==${test1.id }">
<li ><a href="#" class="active" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
<input type="checkbox" value="${test.id}">${test.name }</a></li>
</c:when>
<c:otherwise>
<li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
<input type="checkbox" value="${test.id}">${test.name }</a></li>
</c:otherwise>
</c:forEach>
<%-- <li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
<input type="checkbox" value="${test.id}">${test.name }</a></li> --%>
</c:forEach>
</ul>
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
but I am getting error in eclipse
Multiple annotations found at this line:
- Missing required attribute
"test"
- Undefined attribute name
"test3"
- Missing required attribute
"test"
- Undefined attribute name
"test3"
Can any body help me in this?

Try comparing the strings this way
<c:when test="${test.id == test1.id }">
you could also use the eq instead of ==
<c:when test="${test.id eq test1.id }">

It should be <c:when test= instead of <c:when test3=
try with c:if as well
<c:set var="id1" value="2" />
<c:set var="id" value="2" />
<c:if test="${id == id1 }">
Equal
</c:if>
OR
<c:set var="id1" value="2" />
<c:set var="id" value="2" />
<c:choose>
<c:when test="${id == id1 }">
Equal
</c:when>
<c:otherwise>
Not equal
</c:otherwise>
</c:choose>
Read more JSTL Core conditional Tag
Note:
complete conditional expression must be enclosed inside single ${}
c:when tag should be enclosed inside the c:choose tag
The <c:choose> works like a Java switch statement in that it lets you choose between a number of alternatives. Where the switch statement has case statements, the <c:choose> tag has <c:when> tags. A a switch statement has default clause to specify a default action and similar way <c:choose> has <c:otherwise> as default clause.

Related

how many c:forEach can we have in 1 jsp file

I am having a trouble with my jsp file at the moment, for somehow when i include pagination.jsp, my website goes like this ( but if i don't, everything works well). Can anybody help me with this ?
Here is my original jsp file:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User Management</title>
<jsp:include page="../Common/inlcude.jsp"/>
</head>
<body>
<jsp:include page="../Common/header.jsp"/>
<jsp:include page="../Common/sidebar.jsp"/>
<h1 class="col-md-10" style="margin-bottom:20px"> List Of Member </h1>
<div class="col-md-10">
<form style="margin-left: 20px;padding-left : 20px;" class="table table-hover col-lg-9 col-md-6 col-sm-3 col-xs-3" method="GET" action="#" >
<input type="text" name="search" id="search" placeholder ='Search user'>
<input type="submit" name='submit' value='search'>
</form>
<table style="margin-left: 0px;padding-left : 0px;" class="table table-hover col-lg-9 col-md-6 col-sm-3 col-xs-3">
<thead>
<th>STT</th>
<th>TenTaiKhoan</th>
<th>HoTen</th>
<th>Email</th>
<th>SDT</th>
<th>AnhDaiDien</th>
<th>DiaChi</th>
<th>GioiTinh</th>
<th>NgaySinh</th>
<th>LoaiTaiKhoan</th>
<th>TrangThai</th>
<th>Edit</th>
<th>Delete</th>
</thead>
<tbody>
<c:forEach var="user" items="${users}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${user[0]}</td>
<td>${user[3]}</td>
<td>${user[4]}</td>
<td>${user[5]}</td>
<td><img src="../Resources/Images/${user[1]}" alt="" id="img1" height="150" width="150"></td>
<td>${user[6]}</td>
<td>${user[7]}</td>
<td>${user[8] == 1?"User":"Admin"}</td>
<td>${user[9]}</td>
<td>${user[10] == 1?"Actived":"Removed"}</td>
<td align="center">
<a href="${user[0]}.htm?ModifyForm">
<img src="../Resources/Images/edit.png">
</a>
</td>
<td align="center"><a onclick="return confirm('Are you sure you want to delete this')"
href="${user[0]}.htm?Delete"><img src="../Resources/Images/delete.png"/></a></td>
<tr>
</c:forEach>
</tbody>
</table>
</div>
<jsp:include page="../Common/pagination.jsp"/>
<jsp:include page="../Common/footer.jsp"/>
</body>
</html>
And here is the pagination.jsp file that i inlcude:
<%--
Created by IntelliJ IDEA.
User: levub
Date: 4/10/2020
Time: 2:40 PM
To change this template use File | Settings | File Templates.
--%>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="col-lg-10" style="text-align: center;">
<c:choose>
<c:forEach var="index" begin="1" end="2">
<c:when test="${page > index && page - 1 == index}">
<a class="button" href="UserManagement.htm?page=${page - 1}"></a>
</c:when>
<c:when test="${page <= index && page + 2 >= index}">
<a class="button" href="UserManagement.htm?page=${index}"></a>
</c:when>
<c:when test="${page < 10 && page + 2 == index && 10 - page > 2}">
<a class="button" href="UserManagement.htm?page=${page + 1}"></a>
</c:when>
</c:forEach>
</c:choose>
</div>
And this what i get from my browser when i include it :
Correct way to use Tag. Also, your nesting of tags is incorrect with c:forEach
<c:choose>
<c:when test="${condition1}">
//do something if condition1 is true
</c:when>
<c:when test="${condition2}">
//do something if condition2 is true
</c:when>
<c:otherwise>
//Statements which gets executed when all <c:when> tests are false.
</c:otherwise>

How can I add value fields to the drop down items?

I have a drop down with Small Medium and Large (code below).
I want to capture some text for each line - e.g. Small Tom; Medium Dick; Large Harry.
Supplementary question - can I get more than one name per size - e.g. Small Quantity 3 ;Tom, Dick, Harry
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick" /> <input type="hidden" name="hosted_button_id" value="B98VUHC4Y2HSU" />
<table>
<tr>
<td><input type="hidden" name="on0" value="Size" />Size
</td>
</tr>
<tr>
<td><select name="os0">
<option value="Small (S)">Small (S) </option>
<option value="Medium (M)">Medium (M) </option>
<option value="Large (L)">Large (L) </option>
</select>
</td>
</tr>
</table><input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_cart_LG.gif" border="0"
name="submit" alt="PayPal – The safer, easier way to pay online!" />
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif"
width="1" height="1" />
</form>
No you cannot do that. Check out some information referenced here in another thread. You could try using some sort of bootstrap version of it. It isn't possible with just HTML.
As for that extra question, would you want to? You would have to parse through the return string to separate the values.
Here is a Bootstrap version of it from that thread.
HTML:
<ul class="nav" role="navigation">
<li class="dropdown"> --Select Country--<b class="caret"></b>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop2">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">USA</a>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">UK</a>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Russia</a>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Others <input type="text" id="other"/></a>
</li>
</ul>
</li>
</ul>
JQuery:
$('.dropdown-menu input').click(function (e) {
e.stopPropagation();
});
$('.dropdown-menu li').click(function(){
$('.dropdown-toggle b').remove().appendTo($('.dropdown-toggle').text($(this).text()));
});
Bootstrap JS Example
I accept that what I asked for is not possible or at least not in my competence.
I have realised that what I really want is different The more I thought about it the easier it got.
But for any future enquirer
I need to add one line in the cart for each unique combination of two variables OS0 (Size) and OS1 (Name) and Paypal does that anyway. Only if the pair are duplicated does it bump up the quantity on the existing line.
I call this one closed

What to escape XSS (JSTL)

I'm working on a small JSTL project and I want to prevent xss, the first step I want to take is escaping in html.
Is there a rule what I should escape and what not?
For example the following code prints out a form from the databasepoints
<c:forEach var="player" items="${players}" varStatus="counter">
<li>
<label for="<c:out value="${counter.count}"/>" >${player.email}:</label>
<input class="values" id="<c:out value="${counter.count}"/>" type="number" name="<c:out value="${player.id}"/>" ondblclick="fillInInput(this)" autocomplete="off" placeholder="<c:out value="${player.points}"/>">
</li>
</c:forEach>
This prints some error messages:
<c:if test="${errors.size() >0}">
<div id="errors">
<c:forEach var="error" items="${errors}">
<p><c:out value="${error}"/></p>
</c:forEach>
</div>
</c:if>
This prints a list with rewards from the database
<c:forEach var="reward" items="${rewards}">
<tr>
<td><c:out value="${reward.name}"/></td>
</tr>
</c:forEach>
This prints a checkbox list with rewardtypes which the user can choose from:
<c:forEach var="reward" items="${rewards}" varStatus="counter">
<li><input id="<c:out value="${counter.count + 100}"/>" class="rewards" type="checkbox" onchange="checkButton('button2','rewards')" name="rewardType" value="<c:out value="${reward}"/>" checked>
<label for="<c:out value="${counter.count + 100}"/>"><c:out value="${reward}"/></label></li>
</c:forEach>
Right now I've simply escaped everything,but it doesn't feel right. Can anyone tell me if there is some rule or something so I know what I should escape and what not?
Thanks in advance.

Font-awesome <i> tags auto-generated in JSP

I'm trying to include SB-Admin2 theme using a Spring Roo generated project (using bootstrap, tiles and JSP) but something is going wrong. I have icons corresponding to fa-search font-awesome tags that are displayed on the page.
It seems, that they have been generated when JSP tiles have been processed because they do not appear in my code.
<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li class="sidebar-search">
<div class="input-group custom-search-form">
<input type="text" class="form-control" placeholder="Search..."></input>
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<i class="fa fa-search"></i>
</button>
</span>
</div>
<!-- /input-group -->
</li>
<tiles:insertAttribute name="menu" ignore="true" />
</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
File "menu.tagx" is :
<c:if test="${empty render or render}">
<li>
<jsp:doBody />
</li>
</c:if>
And file "items.tagx" is:
<c:if test="${empty label}">
<spring:message code="menu_item_${fn:toLowerCase(fn:substringAfter(id,'_'))}_label" var="label" htmlEscape="false" />
</c:if>
<c:if test="${not empty messageCode}">
<spring:message code="${messageCode}" var="label" arguments="${label}" htmlEscape="false" />
</c:if>
<li>
<spring:url value="${url}" var="menu_item_url"/>
<a href="${menu_item_url}" title="${fn:escapeXml(label)}">
<c:out value="${label}"/>
</a>
</li>
Generated code in Chrome inspector is:
<div role="navigation" class="navbar-default sidebar">
<div class="sidebar-nav navbar-collapse">
<ul id="side-menu" class="nav">
<li class="sidebar-search">
<div class="input-group custom-search-form">
<input placeholder="Search..." class="form-control" type="text">
<span class="input-group-btn">
<button type="button" class="btn btn-default">
<i class="fa fa-search"></i></button>
</span>
</div>
</li>
<div version="2.0" id="menu">
<li><h2><i class="fa fa-search">Party</i></h2>
<ul>
<li><i class="fa fa-search"><a title="Create new Party" href="/demo/partys?form">Create new Party</a></i></li>
<li><i class="fa fa-search"><a title="List all Partys" href="/demo/partys?page=1&size=10">List all Partys</a></i></li>
<li><i class="fa fa-search"><a title="Find by Last Name" href="/demo/partys?find=ByLastName&form&page=1&size=10">Find by Last Name</a></i></li>
</ul>
</li>
</div>
</ul>
</div>
</div>
Where do the <i class="fa fa-search"> elements come from? How to remove them?
Thank you very much for your help. Denis
Thank you for your tips ! I have finally found where it comes from...
If you don't put content (for instance a comment) between opening and closing tags processed by font-awesome CSS, parser will generate icons in front of all text areas displayed.
To avoid this, you must properly declare tags in your JSP like this:
<i class="fa fa-search"><!-- Some content --></i>
And not:
<i class="fa fa-search"></i>
Hope this help.

Build dynamic checkboxes

Hey guys I have about 8 fieldSets and Im iterating over a list. I want to fill up the checkboxes based off a value of each iteration for ex.
<c:if test="${detBean.groupName == 'HEADER_DATA}">
*Add that checkbox to that fieldset and so on...
<c:forEach var="detBean" items="${detFields}">
Display Name -- ${detBean.displayName}
Field Name -- ${detBean.fieldName}
Group Name -- ${detBean.groupName}
</c:forEach>
<tr>
<td>
<div id="displayFields" style="display:block;">
<fieldset class="det">
<legend>Header Data</legend>
<input type="checkbox" name="${detBean.displayName}
" value="${detBean.displayName}
">${detBean.displayName}
</input>
</fieldset>
<fieldset class="det">
<legend>Materiel Data</legend>
<input type="checkbox" name="${detBean.displayName}
" value="${detBean.displayName}
">${detBean.displayName}
</input>
<br/>
</fieldset>
</td>
</tr>
Thankx
This worked:
<div id="displayFields" style="display:block;">
<fieldset class="det">
<legend>Header Data</legend>
<c:forEach var="detBean" items="${detFields}">
<c:if test="${detBean.groupName == 'HEADER_DATA'}">
<input type="checkbox" name="${detBean.displayName}" value="${detBean.displayName}">${detBean.displayName}</input>
<br/>
</c:if>
</c:forEach>
</fieldset>
<fieldset class="det">
<legend>Materiel Data</legend>
<c:forEach var="detBean" items="${detFields}">
<c:if test="${detBean.groupName == 'MATERIEL_DATA'}">
<input type="checkbox" name="${detBean.displayName}" value="${detBean.displayName}">${detBean.displayName}</input>
<br/>
</c:if>
</c:forEach>
</fieldset>

Resources