String checking in jstl error - jstl

I have to check a attribute from server , and depending upon the value, i have to set the color of the font. The code i used for the same is: I am very new in jstl. Can handle this with javascript or jQuery, but have a constraint , not to use js here. :(
<c:if test="${error != null}">
<c:choose>
<c:when test="${error_code eq 'failed'}">
<p align="left"><font size="3" color="red">${error}</font></p>
</c:when>
<c:when test=test="${error_code eq 'success'}">
<p align="left"><font size="3" color="green">${error}</font></p>
</c:when>
</c:choose>
</c:if>
And the server side code is:
if (result) {
request.setAttribute("error_code","success");
request.setAttribute("error","Object successfully created.");
} else {
request.setAttribute("error_code","failed");
request.setAttribute("error","Object creation failed.");
}
what i am doing wrong here:
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
org.apache.jasper.JasperException: /tool/Content/content.jsp (li
ne: 68, column: 18) **quote symbol expected**
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorH
andler.java:42)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.j
ava:443)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.j
ava:89)

Following line is incorrect. You have write "test" two times ;-)
<c:when test=test="${error_code eq 'success'}">

Now its working , I changed the code as:
<c:if test="${error != null}">
<c:choose>
<c:when test="${fn:containsIgnoreCase(error_code, 'failed')}">
<p align="left"><font size="3" color="red">${error}</font></p>
</c:when>
<c:when test="${fn:containsIgnoreCase(error_code, 'success')}">
<p align="left"><font size="3" color="green">${error}</font></p>
</c:when>
</c:choose>
</c:if>

Related

Why does my conditional display of 2D array elements fail?

I have a 2-dimensional array, which can be done like this (working code):
<c:forEach items="${dataArray}" var="row">
Subject id: ${row[0]}
</c:forEach>
But I want to add a condition; when I try this it does not display anything:
<c:forEach items="${dataArray}" var="row">
<c:choose>
<c:when test="${fn:containsIgnoreCase(${row[0]}, 'condtion1')}">
Subject id: ${row[0]}
</c:when>
</c:choose>
</c:forEach>
Have I got something wring in the test attribute that makes it always false?

Custom sort for String with 3 set of values in datatables

I have used datables on one of my html tables. There are multiple columns currenltly in it.
One of the columns in it can contain one of the below three values:
Amber
Pending
Red
I want to implement sorting such that all rows with value Pending are seen first, then Amber and then Red. (Cannot use the default ascending and descending sorting as the order will not be correct then)
code Snippet:
JSP (Table creation)
<table class="tableContent nofx cell-border compact" id="violationTable">
<thead>
<tr>
<th class="col1"><i18n:message key="rule.name" /></th>
<th class="col2"><i18n:message key="rule.value" /></th>
<th class="col3"><i18n:message key="rule.isr.value" /></th>
<th class="col4"><i18n:message key="rule.status" /></th>
</tr>
</thead>
<tbody>
<c:forEach items="${ruleViolationList}" var="i" varStatus="loopStatus">
<tr data-rule-id="<c:out value="${i.id}" />" data-country-id="<c:out value="${i.countryId}" />"
>
<td class="col1">
<c:out value="${i.PolicyRule}" />
</td>
<td class="col2">
<c:out value="${i.RuleValue}" escapeXml="false" />
</td>
<td class="col3">
<c:out value="${i.isrValue}" />
</td>
<c:choose>
<c:when test="${i.violationTypeId == 1}">
<td class="red status" >
<i18n:message key="rule.violation.red" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 2}">
<td class="amber status" >
<i18n:message key="rule.violation.amber" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 4}">
<td class="blue status" >
<i18n:message key="rule.violation.dispensation.approval.pending" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 5}">
<td class="amber status" >
<i18n:message key="rule.violation.amber" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 6}">
<td class="red status" >
<i18n:message key="rule.violation.red" />
</td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</tbody>
</table>
Servlet:
ArrayList<RuleViolation> ruleViolationList = daoFactory.getRuleViolationsDAO().list();
request.setAttribute("ruleViolationList", ruleViolationList);
JS:
$(document).ready(function() {
$('#violationTable').DataTable();
});
So what i ideally want is, when the table is shown on page the data should be sorted alphabetically based on data in first column as well as data in last column (i.e Pending, Amber and then Red).
Check out the snippet, you can highly customize how your column is filtered/sorted using the option columns.render.
The example I gave is not the smartest, but gives you one idea of what you can make. I advice you to use "Nested Object data" if you choose this approach.
var dataSet = [
['Name1', 3, 'Red'],
['Name2', 2, 'Amber'],
['Name3', 1, 'Pending']
];
$(document).ready(function() {
$('#example').DataTable( {
data: dataSet,
columns: [
{ title: "Name" },
{ title: "Value" },
{
title: "Types",
"render": function ( data, type, full, meta ) {
if (type == 'sort') {
if (data == 'Red') return 3;
else if(data == 'Pending') return 1;
else if(data == 'Amber') return 2;
} else {
return data;
}
}
}
]
} );
} );
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" width="100%"></table>

Displaying list in a list JSP Page

I am trying to display a list and every object of that list has another list which I have to display, the values are coming but are not being displayed, below is the code I have tried if someone could identify what I am doing wrong, it will be very helpful. I am using Spring MVC but I don't think it has anything to do with this error, any help will be appreciated.
#Entity
#Table(name="FILE_TRACKING_MANAGEMENT")
#NamedQuery(name="FileTrackingManagement.findAll", query="SELECT f FROM FileTrackingManagement f")
{
#OneToMany(mappedBy="fileTrackingManagement")
private List<FileNoting> fileNotings;
//bi-directional many-to-one association to FileTrackingFile
#OneToMany(mappedBy="fileTrackingManagement")
private List<FileTrackingFile> fileTrackingFiles;
}
My Controller Class:
#RequestMapping("viewMarkedFiles/list")
public ModelAndView viewMarkedFiles(Model model)
{
List<FileTrackingManagement> markedFileList= fileService.getList();
//model.addAttribute("markedFileList", markedFileList);
return new ModelAndView("viewMarkedFiles", "markedFileList", markedFileList);
}
My JSP Page:
<table border="1" bgcolor="black" width="600px">
<c:forEach items="${markedFileList}" var="markedFileVal">
<%-- <c:set var="fileNotings" value="${jobs[i.index].jobId}"/> --%>
<tr
style="background-color: white; color: black; text-align: center;"
height="30px">
<td><c:out value="${markedFileVal.diarynumber}" />
<td><c:out value="${markedFileVal.subject}" />
<td>
</td>
<td>
</td>
<td>
<c:forEach items="${markedFileList.fileNotings}" var="filenotings">
<c:out value="${filenotings.notingData}" /></c:forEach>
</td>
</c:forEach>
</table>
It throws this exception:
45: <%-- <c:out value="${fileVal.description}" /> --%>
46: </td>
47: <td>
48: <c:forEach items="${markedFileList.fileNotings}" var="filenotings">
49: <c:out value="${filenotings.notingData}" /></c:forEach>
50:
51:
Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "fileNotings"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Thanks in advance
<c:forEach items="${markedFileList}" var="markedFileVal">
So, you're iterating oover the list markedFileList. Inside the body of the tag, the current element is markedFileVal.
Then you do
<c:forEach items="${markedFileList.fileNotings}" var="filenotings">
So you're ietarting over markedFileList.fileNotings. But markedFileList is a list. It's not the current element. What you want is
<c:forEach items="${markedFileVal.fileNotings}" var="filenoting">
Also note that I removed the final s from filenotings. This variable points to a single filenoting. Not to several ones.
#OneToMany(fetch = FetchType.EAGER, mappedBy="fileTrackingManagement")
private List fileNotings;
The value of fetchType is Lazy by default, when I changed its value, it worked

bean value show correctly, but add logic in jsp and it fails

The property is set in the bean, updated in the DAO, and appears correctly in the jsp when set as the following:
<html:hidden property="user.strInfoLocked" value="${user.strInfoLocked}" />
When I use developer tools, the value shows as follows:
<input type="hidden" name="user.strInfoLocked" value="true">
But I can't get the value to be used in a conditional statement. I've tried this:
<c:choose>
<c:when test="${user.strInfoLocked eq 'true'}">TRUE </c:when>
<c:when test="${user.strInfoLocked eq 'false'}">FALSE </c:when>
</c:choose>
and I've tried this:
<c:if test= "${user.strInfoLocked == 'true'}">
<p>My value is TRUE<p>
</c:if>
<c:if test= "${user.strInfoLocked == 'false'}">
<p>My value is FALSE<p>
</c:if
and in both cases, the whole thing is skipped.
Do I need to declare this somewhere else, apart from the bean? It's just odd that the values are showing up correctly, but then when I add it to logic, the whole thing goes bezerk.
Both eq and == should work. I suggest you display the value from user.strInfoLocked and be sure that it has the value 'true' or 'false'.
Boolean should work, too, if you define the field in your bean as boolean.
<c:choose>
<c:when test="${result.strInfoLocked}">Locked</c:when>
<c:when test="${!result.strInfoLocked}">Not Locked</c:when>
</c:choose>
I have tested the code as a string and as a boolean. Both work.
This seems to be a type. You missed out space before eq in below statement and you need eq for the same:
<c:choose>
<c:when test="${user.strInfoLockedeq 'true'}">TRUE </c:when>
<c:when test="${user.strInfoLockedeq 'false'}">FALSE </c:when>
</c:choose>
Chnage it to :
<c:choose>
<c:when test="${user.strInfoLocked}">TRUE </c:when>
<c:when test="${user.strInfoLocked}">FALSE </c:when>
</c:choose>

how to use if loop inside if loop using jstl

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

Resources