<c:if test='${not empty "${records}"}'> never evaluates false - java

I am having a problem with JSTL and empty operator. I already made few simple pages and everything worked fine, but now I have:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<form action="/Projekt/myaccount" method="post">
<table border="1">
<tr>
<td>Artist</td>
<td>Record Name</td>
<td>Delete</td>
</tr>
<c:forEach var="item" items="${records}">
<tr>
<td>${item.artist}</td>
<td>${item.recordName}</td>
<td>
<input type="checkbox" name='${item.recordName}|${item.recordName}'/>
</td>
</tr>
</c:forEach>
</table>
<hr/>
<input type="submit" name="back" value="back"/>
<c:if test='${not empty "${records}"}'>
<input type="submit" name="delete" value="delete selected"/>
</c:if>
</form>
</body>
</html>
now no matter if I set the records attribute or not, the delete button shows up:
<c:if test='${not empty "${records}"}'>
<input type="submit" name="delete" value="delete selected"/>
</c:if>
in normal situation to records attribute I pass ArrayList and then use foreach, but sometimes ArrayList is empty, so in those situations I don't want delete button to show up, I fought that easiest way to achieve this would be to use this empty operator. Where am I making a mistake?
I even tried to manually set this attribute to null:
if (ar.size() != 0)
request.setAttribute("records", ar);
else
request.setAttribute("records",null);
EDIT:
#Qwe: yes you are right, it worked for me before because I tested if attribute was empty in my way, it was always true, because I used wrong construct, but it worked because I just wanted to show one string, if there was no String nothing showed up so I was thinking that everything worked fine.

<c:if test='${not empty "${records}"}'> as well as <c:if test="${!empty '${showWarning}'}"> (from your comment) will always resolve to true because you're are actually testing if a String ${records} is empty or not, and obviously it is not.
Just to be sure - by String ${records} I mean a String value, just as if you were assigning it in Java like String foo = "${records}";.
The next line of code will test if records variable (that is looked up from page, request, session or application scope) is empty or not:
<c:if test="${not empty records}">
The line of code is 100% guaranteed to work :)
Also, request.setAttribute("records",null) is a bad way to remove attributes because empty tests not just request scope, but page, session etc. Use <c:remove var='records'/> instead.

Related

Use ArrayList with servlet and jsp? [duplicate]

I am working on a project to try and teach myself spring and struts. I am currently stuck on a JSP page. I have a pojo class with variables eid and ename with getters/setters, I also have a table in sql with the same values with six populated rows.I am accessing my database through a JdbcTemplate and have stored the result in a list, I then passed this list to my action page in which I set it as a request.setAttribute("empList",eList). In my jsp page I call that attribute and then try to iterate through it using JSTL. However nothing shows up, I know that my list variable has data in it since i checked it using the expression tag <%=eList%> and objects show up like this:
[org.classes.database.Employee#d9b02,
org.classes.database.Employee#13bce7e,
org.classes.database.Employee#171cc79,
org.classes.database.Employee#272a02,
org.classes.database.Employee#137105d,
org.classes.database.Employee#1359ad]
I thought that maybe I was missing something on jstl but I have jstl-1.2 in my META-INF/lib folder. I have also tried to add it in the configure path file and still nothing. I also have the correct tag url. Also when I do a simple <c:out value="Hello"/>. Hello does print out. So this leads me to believe that my jstl is working properly, but when I try iterating through my list using jstl nothing shows up at all.Anyways here is my JSP page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page import="java.util.List"%>
<!DOCTYPE html>
<% List eList = (List)session.getAttribute("empList");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
<c:out value="Hello"></c:out>
<h3>Employee Details</h3>
<hr size="4" color="gray"/>
<table>
<%=eList%>
<c:forEach items="${eList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Any help would be highly appreciated!
Before teaching yourself Spring and Struts, you should probably dive a little deeper into the Java language. Output like this
org.classes.database.Employee#d9b02
is the result of the Object#toString() method which all objects inherit from the Object class, the superclass of all classes in Java.
The List sub classes implement this by iterating over all the elements and calling toString() on those. It seems, however, that you haven't implemented (overriden) the method in your Employee class.
Your JSTL here
<c:forEach items="${eList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
is fine except for the fact that you don't have a page, request, session, or application scoped attribute named eList.
You need to add it
<% List eList = (List)session.getAttribute("empList");
request.setAttribute("eList", eList);
%>
Or use the attribute empList in the forEach.
<c:forEach items="${empList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
change the code to the following
<%! List eList = (ArrayList)session.getAttribute("empList");%>
....
<table>
<%
for(int i=0; i<eList.length;i++){%>
<tr>
<td><%= ((Employee)eList[i]).getEid() %></td>
<td><%= ((Employee)eList[i]).getEname() %></td>
</tr>
<%}%>
</table>
you can read empList directly in forEach tag.Try this
<table>
<c:forEach items="${sessionScope.empList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
</table>
<c:forEach items="${sessionScope.empL}" var="emp">
<tr>
<td>Employee ID: <c:out value="${emp.eid}"/></td>
<td>Employee Pass: <c:out value="${emp.ename}"/></td>
</tr>
</c:forEach>
another example with just scriplets, when iterating through an ArrayList that contains Maps.
<%
java.util.List<java.util.Map<String,String>> employees=(java.util.List<java.util.Map<String, String>>)request.getAttribute("employees");
for (java.util.Map employee: employees) {
%>
<tr>
<td><input value="<%=employee.get("fullName") %>"/></td>
</tr>
...
<%}%>

How to properly include a jsp in another jsp

I'm making a form to add events to a list but I need to be able to see in that form the previous events from that list. I have a jsp that shows a list of events but it takes an attribute usually added by the controller when you access the list directly from the browser.
So how can I add the list jsp filling that attribute so it shows a list and not just the headers?
I have alreay included the jsp using
<jsp:include page="comp_list.jsp"></jsp:include>
And it shows the headers but as there is no attribute it shows no list. I tried adding attributes to the include like:
<jsp:include page="comp_list.jsp">
<jsp:attribute name="compensaciones">
${compensaciones}
</jsp:attribute>
</jsp:include>
But when I do it it shows an error telling me that this cannot be done.
Also tried params but that would not be the answer for me because params are treated in the controller and not in the jsp itself.
I'm just getting the header of the list which is fine, but i would like to see the list.
Edit: this is how i build the list in case you are wondering
<tbody>
<c:forEach var="comp" items="${compensaciones}">
<td>${comp.getSomething()}</td>
...
</c:forEach>
</tbody>
<jsp:include page="pagename.jsp" />
i thing you are not provide extension in your include tag
Sometimes since I've had prior experience back in the days with HTML and PHP, we simply just would include things like navigation and header for easier management.
I'm unsure for what purpose you're including JSP, but here's an example of how I've done it since I include JSP if a boolean is true.
The site that is accessed:
<div class="row">
<div class="col-lg-2">
</div>
<div class="col-lg-8">
<%
if (loggedin) {
%>
<%# include file="includes/profile.jsp" %>
<% } else {
out.println("<div><h2>You aren't logged in!</h2></div>");
}
%>
</div>
<div class="col-lg-2">
</div>
</div>
And the top and bottom of the JSP file I'm including doesn't contain things like head, title, html, body etc.
<%
User currUser = null;
currUser = (User) session.getAttribute("user");
%>
<div>
<h2>Du er logget ind, velkommen <% out.println(currUser.getUsername().toUpperCase()); %></h2> <br>
<h5>Din Saldo: <b><% if (currUser.getBalance() < 0) { out.println("<font color='red'>" + currUser.getBalance() + "</font>");} else { out.println("<font color='green'>" + currUser.getBalance() + "</font>");} %></b></h5>
<br>
<form class="form" method="post" action="#">
<h4>Oplysninger: </h4>
<h6>
For at ændre oplysninger skal du indtaste dit kodeord!
</h6>
Nuværende Kode: <input type="password" class="form-control" placeholder="kodeord" name="password" required>
<hr>
Email: <input type="text" class="form-control" value="<% out.println(currUser.getEmail()); %>" name="email"> <br>
Brugernavn: <input type="text" class="form-control" value="<% out.println(currUser.getUsername()); %>" name="username"> <br>
<input type="submit" class="btn btn-default" value="Indsend Oplysninger" />
</form>
</div>
When importing JSP into JSP, it's important to know that they will conflict if 1. Duplicate Local Variables. 2. Certain HTML tags aren't correct.
Your intended use seems very complicated to me since I'm not quite understanding it, sorry :3 but if you can't get it working consider to throw it to a session and if it's a one time use then remove it once you used it.
This is working from dacades
<%# include file="../scripts/standard_head_declaration.jsp" %>

Can you make the id dynamic - <div id=[dynamic field here]

I am trying to loop through an Arraylist on my JSP without any luck.
I am able to loop through this successfully but only hit this error while trying to dynamically change the "
I have tried all different approaches by opening and closes the tags, trying different enclosure characters namely: single quotes and double quotes.
The error that I am getting is as follows:
javax.el.MethodNotFoundException: Method not found: class java.util.ArrayList.getChartName()
at javax.el.Util.findWrapper(Util.java:351)
at javax.el.Util.findMethod(Util.java:214)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:174)
This what my JSP looks like:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#include file="header.jsp" %>
<div id="content_main">
<c:if test="${not empty dashboardDetail}"></c:if>
<c:forEach var="dashboardDetailList" items="${dashboardDetail}">
<div id="${dashboardDetail.getChartName()}" style="width: 100%; height: 400px;"> </div>
</c:forEach>
</div>
<%#include file="footer.jsp" %>
From the names you've used, you've got them the wrong way around.
<c:forEach var="dashboardDetailList" items="${dashboardDetail}">
items should be the list and var the current element. But you've referred to dashboardDetail correctly inside the loop. Switch the names (for every reference to the list):
<c:forEach var="dashboardDetail" items="${dashboardDetailList}">
The error occurs because as you have it now, you are calling getChartName() on an ArrayList, and that method doesn't exist.
Try this:
<c:forEach var="dashboardDetailObject" items="${dashboardDetail}">
<div id="${dashboardDetailObject.chartName}"
style="width: 100%; height: 400px;"></div>
</c:forEach>
dashboardDetailList changed to dashboardDetailObject

How to access OGNL tracing evaluations in Struts 2?

I was told to optimize a web application. To do so I use JProfiler.
I noticed a large part of the response time is spent in the presentation layer. Especially, when the code builds the HTML code.
Drilling down this bottle neck, I saw that the code deals a lot with OGNL classes/methods (nearly 50% of the calls).
I tuned the JSP code (see below for details) by removing some unnecessary OGNL statements. I managed to gain 200ms. However the code still spend more than 2000ms on average in this bottle neck. The overall response time is around 3000ms.
The OGNL documentation says this:
As of OGNL 2.5.0 the OgnlContext object can automatically tracks evaluations of expressions. (...) you can access the last evaluation through the lastEvaluation property of OgnlContext.
Since I use Struts 2.3.15.1 (OGNL v3.0.6) how can I access this property or call the getLastEvaluation of OgnlContext either from JSP file or Action class ?
JSP file
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix ="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<title><s:text name="domain.domain0.title"/></title>
<script type="text/javascript" src="<s:url value='/js/domain/domain0.js'/>"></script>
<script type="text/javascript" src="<s:url value='/js/core/html.js'/>"></script>
</head>
<body>
<div style="width:100%;color:green;font-weight: bold;text-align:center">
<s:text name="domain.domain0.information"/>
</div>
<div class="content selectFen">
<s:set var="noAnchor">false</s:set>
<s:iterator value="artefactList" status="numRow">
<s:if test="court == nomenclatureCritere && #noAnchor!=true">
<s:set var="noAnchor">true</s:set>
<a id="NomencAnchor" href="#NomencAnchor" style="visibility:hidden;"></a>
</s:if>
<s:set var="isOpen" value="nomenclatureCritere.startsWith(court) || ligProd != '80'"/>
<div class="parcoursNomenc">
<table onmouseover='this.style.backgroundColor="gainsboro"' onmouseout='this.style.backgroundColor=""' style="width: 100%;font-weight:bolder">
<tbody>
<tr>
<s:url var="childNomenUrl" action="ajaxPopupNomenChild" escapeAmp="false">
<s:param name="dateCritere" value="dateCritere"/>
<s:param name="sidNomenclaturePere" value="sidArtNomenc"/>
</s:url>
<s:if test="#isOpen">
<s:if test="#numRow.last">
<td onclick="noeudPlusMoins(this);loadChild('<s:property value="childNomenUrl" />', 'divParcours<s:property value="sidArtNomenc"/>')" class="open_last"> </td>
</s:if>
<s:else>
<td onclick="noeudPlusMoins(this);loadChild('<s:property value="childNomenUrl" />', 'divParcours<s:property value="sidArtNomenc"/>')" class="open"> </td>
</s:else>
</s:if>
<s:else>
<s:if test="#numRow.last">
<td onclick="noeudPlusMoins(this);loadChild('<s:property value="childNomenUrl" />', 'divParcours<s:property value="sidArtNomenc"/>')" class="closed_last"> </td>
</s:if>
<s:else>
<td onclick="noeudPlusMoins(this);loadChild('<s:property value="childNomenUrl" />', 'divParcours<s:property value="sidArtNomenc"/>')" class="closed"> </td>
</s:else>
</s:else>
<td style="vertical-align: top; width: 8.5em; padding-top: 5px;">
<s:text name="domain.domain0.label"/> <s:property value="court"/>
</td>
<td class="description">
<s:property value="description"/>
</td>
</tr>
</tbody>
</table>
<div id="divParcours<s:property value="sidArtNomenc"/>" class="<s:if test="numRow.last">subtree_last</s:if><s:else>subtree</s:else>" >
<s:if test="#isOpen">
<s:action name="popupNomenExist" executeResult="true">
<s:param name="dateCritere" value="dateCritere"/>
<s:param name="nomenclatureCritere" value="nomenclatureCritere"/>
</s:action>
<s:action name="ajaxPopupNomenChild" executeResult="true">
<s:param name="dateCritere" value="dateCritere"/>
<s:param name="sidNomenclaturePere" value="sidArtNomenc"/>
</s:action>
</s:if>
</div>
</div>
</s:iterator>
</div>
</body>
</html>
You can access OgnlContext like
OgnlContext context = (OgnlContext) ActionContext.getContext().getValueStack().getContext();
context.setTraceEvaluations(true);
Evaluation evaluation = context.getLastEvaluation();
How to trace evaluations is described in the document you linked in the section for Trace Evaluations
You can also set system property ognl.traceEvaluations before the context is instantiated to turn on this feature.
Looking at your JSP code, it's evident that the culprits are the <s:action> tags:
You call two actions on each iteration... that basing on the number of rows you are iterating, may become a serious problem.
I would try refactoring that section; a big part of the bottleneck is in the mechanism to interpret the tag, call the action, pass through the Interceptor Stack, and so on.
If you can generate the same output minimizing the calls to actions, the bottleneck would decrease significantly.
P.S: a very minimal optimization is to change the order of the boolean and string comparison in your s:if, to exploit the short circuit (boolean comparison is faster):
<s:if test="#noAnchor!=true && court == nomenclatureCritere">
EDIT
It works better without s:action however the page need them and I don't know how to replace them
To be more clear, an Action should
perform one (or few) logical action;
never be used as an helper class.
When you call popupNomenExist , your only requirements is to call the server, do something, and receive the result to inject it in the JSP.
There are several ways to receive that result, and calling an Action is probably the worst (in terms of performances, because of the reasons listed above).
You can take the code from the execute() method of your popupNomenExist Action, and :
place it in a method of YOUR current action, like described in RomanC linked answer;
place it in a static method of an helper class;
precalculate it in your current action, and expose the results as List of Strings;
and so on...
In addition, we can't argue what is exactly being performed inside your popupNomenExist's execute() method, but I guess there are chance that it could be refactored to prevent executing some part of logic that is common to each iteration, grouping them and executing before the iteration as a sort of cache.

How to use session in JSP pages to get information?

I have a JSP page used for editing some user's info. When a user logins to the website, I keep the information in the session, then in my edit page I try the following:
<%! String username=session.getAttribute("username"); %>
<form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
</form>
but it gives error saying session cannot be resolved. What can I do about it?
JSP implicit objects likes session, request etc. are not available inside JSP declaration <%! %> tags.
You could use it directly in your expression as
<td>Username: </td>
<td><input type="text" value="<%= session.getAttribute("username") %>" /></td>
On other note, using scriptlets in JSP has been long deprecated. Use of EL (expression language) and JSTL tags is highly recommended. For example, here you could use EL as
<td>Username: </td>
<td><input type="text" value="${username}" /></td>
The best part is that scope resolution is done automatically. So, here username could come from page, or request, or session, or application scopes in that order. If for a particular instance you need to override this because of a name collision you can explicitly specify the scope as
<td><input type="text" value="${requestScope.username}" /></td> or,
<td><input type="text" value="${sessionScope.username}" /></td> or,
<td><input type="text" value="${applicationScope.username}" /></td>
Use
<% String username = (String)request.getSession().getAttribute(...); %>
Note that your use of <%! ... %> is translated to class-level, but request is only available in the service() method of the translated servlet.
See how JSP code is translated to a servlet.
The reason why you are getting the compilation error is, you are trying to access the session in declaration block (<%! %>) where it is not available. All the implicit objects of jsp are available in service method only. Code of declarative blocks goes outside the service method.
I'd advice you to use EL. It is a simplified approach.
${sessionScope.username} would give you the desired output.
Suppose you want to use, say ID in any other webpage then you can do it by following code snippet :
String id=(String)session.getAttribute("uid");
Here uid is the attribute in which you have stored the ID earlier. You can set it by:
session.setAttribute("uid",id);
<%! String username=(String)session.getAttribute("username"); %>
form action="editinfo" method="post">
<table>
<tr>
<td>Username: </td><td> <input type="text" value="<%=username %>" /> </td>
</tr>
</table>
add <%! String username=(String)session.getAttribute("username"); %>
You can directly use (String)session.getAttribute("username"); inside scriptlet tag ie <% %>.
form action="editinfo" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input type="text" value="<%if( request.getSession().getAttribute(" parameter_whatever_you_passed ") != null
{
request.getSession().getAttribute("parameter_whatever_you_passed ").toString();
}
%>" />
</td>
</tr>
</table>
</form>

Categories