Display a datatable using jstl - java

In my java web application, I have a data table called "sell"
sell(id_sell,id_buyer,id_product,date,final_price,shipping_price,sales_tax)
I have created a jsp page which display some information about the user, such as the products which won at the auction; these products are described by "sell". In this page I have to use jstl library and my idea is to use 'c:forEach' tag to iterate every row of the table.
Here is my code:
<table class="table table-bordered">
<thead>
<tr>
<th>Product</th>
<th>Date</th>
<th>Final Price</th>
<th>Shipping Price</th>
<th>Salex Tax</th>
</tr>
</thead>
<tbody>
<c:forEach var="s" items="${sell}">
<tr>
<td><c:out value="${s.id_product}"/></td>
<td><c:out value="${s.date}"/></td>
<td><c:out value="${s.final_price}"/></td>
<td><c:out value="${s.shipping_price}"/></td>
<td><c:out value="${s.sales_tax}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
But this code shows only an empty table. Where am I doing wrong? Do I have to import something?

From your servlet, load the needed data from your data source, and add it to a scope. Your JSP will then be able to access it through an EL expression. For example, to add data in the request scope:
List<> data = yourDao.list();
request.setAttribute("sell", data);
About your comment on your own question, use the session scope wisely. Good practice is to use the smallest scope possible.

Related

Thymeleaf: th:each for two table rows?

How can I, inside a th:each loop create a group of two rows instead of just one?
I know I can do:
<tr th:each="obj: ${listOfObjects}">
<td>a column with data: ${obj.id}</td>
</tr>
However, I want two <tr> elements to be created, as I would do with JSTL:
<c:forEach items="${listOfObjects}" var="obj">
<tr>
<td>${obj.id}</td>
</tr>
<tr>
<td>${obj.name}</td>
</tr>
</c:forEach>
Is there a way to achieve that with Thymeleaf?
You could use a th:block element for grouping the rows together and repeat them:
<th:block th:each="obj: ${listOfObjects}">
<tr>
<td th:text="${obj.id}"></td>
</tr>
<tr>
<td th:text="${obj.name}"></td>
</tr>
</th:block>
You can read more about th:block here

adding an icon menu from another jsp file as a column to a datatable in liferay

There are 2 tables here:
The table below is using purely container to display populate the tables from database whereas the one above is using datatable.
However i wish to duplicate the last column of displaying a icon menu for edit and delete actions which is contained inside actions.jsp inside the table above.
This is my partial code in view.jsp displaying the datatables.
<%
List<Guestbook> guestbookList = GuestbookLocalServiceUtil.getGuestbooks(scopeGroupId);
request.setAttribute("guestbookList", guestbookList);
%>
<table id="gbdb" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${guestbookList}" var="guestbook">
<tr>
<td>${guestbook.name}</td>
<td>${guestbook.status}</td>
<td><jsp:include page="${actions.jsp}" flush="true" /></td>
</tr>
</c:forEach>
</tbody>
</table>
However as you can see my function of diplaying actions.jsp is not similar to the method for using container where i can just referenced the path using
<td> <liferay-ui:search-container-column-jsp
align="right"
path="/guestbookadminportlet/guestbook_actions.jsp" /></td>
Please help me with the correct way of displaying by referencing to actions.jsp
You should not include whole jsp as an action column if using jQuery data-table,
Let's assume you need to add Delete book action so your td should be replace by following code.
<td>
<portlet:actionURL name="<%=DELETE_BOOK%>" var="deleteBookUrl">
<portlet:param name="bookId" value="${book.id}"/>
</portlet:actionURL>
<input type="button" onclick="location.href='${deleteBookUrl}'" value="Delete"/>
</td>
Hope this will help you.

JSP - loop through table by key value

I have a picture attached with pretty much all the information. To add to it. The table data is stored within a java bean within an ArrayList (Each record is one bean).
I would like my application to be able to recognize that I am in CYCLE_ID n and have all the runs displayed in a table (see Desired Results), and for each CYCLE_ID the same thing.
I am open to making changes in the background java class (not shown), or within the jsp.
<div id="activeQueue">
<h2>Active Modeling Queue for <bean:write name="userBean" property="uid" /> </h2>
<c:forEach items="${activeQueueList}" var="currentQueue">
<p>Queue {<c:out value="${currentQueue.modelQueueID}" />} - <c:out value="${currentQueue.PROJECTION_PROJECT_NME}" /></p>
<table>
<tr>
<th>Type</th>
<th>Start Time </th>
<th>Status</th>
</tr>
<tr>
<td><c:out value="${currentQueue.modelQueueDes}" /></td>
<td><c:out value="${currentQueue.ROW_CREATE_TSP}" /></td>
<td><c:out value="${currentQueue.STATUS_DES}" /></td>
</tr>
</table>
</c:forEach>
First prepare a Map using CYCLE_ID as key and the Queue List as the value. Assuming the bean class is ActiveQueue you can do the following
Map<Integer, ActiveQueue> data = new HashMap<Integer, ActiveQueue>();
for(ActiveQueue queue : activeQueueList) {
if(!data.containsKey(queue.CYCLE_ID)) {
data.put(queue.CYCLE_ID, new ArrayList<ActiveQueue>);
}
data.get(queue.CYCLE_ID).add(queue);
}
Now you can use two nested loops in jsp. The outer loop iterates over the Map data and the inner loop iterates over the corresponding Map.entry values. Like following
<div id="activeQueue">
<h2>Active Modeling Queue for <bean:write name="userBean" property="uid" /></h2>
<c:forEach items="${data}" var="entry">
<p>Queue {<c:out value="${entry.key}" />} - <c:out value="${entry.value[0].PROJECTION_PROJECT_NME}" /></p>
<table>
<tr>
<th>Type</th>
<th>Start Time </th>
<th>Status</th>
</tr>
<c:forEach items=${entry.value} var="currentQueue">
<tr>
<td><c:out value="${currentQueue.modelQueueDes}" /></td>
<td><c:out value="${currentQueue.ROW_CREATE_TSP}" /></td>
<td><c:out value="${currentQueue.STATUS_DES}" /></td>
</tr>
</c:forEach>
</table>
</c:forEach>
Instead of having a List<Run> and iterating on it, you should have a List<Cycle>, where each Cycle contains a List<Run>.
Grouping the original flat list of Runs by Cycle is easy with a Map storing the ID of the cycles as keys, and the corresponding cycle as value:
you iterate over the runs
for each run, you fetch the corresponding cycle by ID in the map. If it's not there yet, you create it and store it in the map
you add the run in the cycle
Ath the end, the keySet() of the map contains all the cycles, and each cycle contains all its runs.
Then use two nested foreach loops in the JSP.

do log-out from a selected session id from a View in spring mvc

I don't know if the titles says what i'm looking for.
Basically i got a table that shows current logged-in sessions. I'd like to be able to change their status and log them out. This is the JSTL code:
<div id="users_table_div">
<table id="box-table" style="width: 690px">
<thead>
<tr>
<th>IP</th>
<th>USER NAME</th>
<th>LAST ACTIVITY</th>
<th>LOGIN STATUS</th>
</tr>
</thead>
<tbody>
<c:forEach var="userDetail" items="${userSessionData}">
<tr>
<td><c:out value="${userDetail.ip}"/></td>
<td><c:out value="${userDetail.username}"/></td>
<td><c:out value="${userDetail.lastActivity}"/></td>
<td><img src="images/sign-out.png" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
Currently i've set a javascript function in the sessionId, but there might be another way to log out a selected user by its sessionId using spring security MVC.
So from the controller, i get different session data from all the users.
IP, Username, LastActivity and their SessionId (to log them out)
Is there a nice way to manage this?
This has been already resolved.
Just in case other is under the same issue:
I just made another #RequestMapping in the Controller with the sessionId passed from the view as the following:
<a href="<c:url value='/endsession?sessionId=${userDetail.sessionId}' />">
Then in the Controller the code was very simple:
#RequestMapping(value="/endsession", method = RequestMethod.GET)
public String endSession(String sessionId, Principal principal) {
logger.info("Removing sessionID = " + sessionId);
sessionRegistry.getSessionInformation(sessionId).expireNow();
return "activeusers";
}
And taraam :D
Hope it helps to others as well.

Spring MVC Error Messages

Spring MVC Error Messages
Hello, Spring Fellows,
I have a form that is validated by the Spring Validation once submitted. Each field on the form may contain multiple errors messages if validation fails, so error messages are displayed below the field, not next to it. Here's the code snippet.
<tr>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
Note that there is a star at the end of the path value to indicate that all error messages for the name must be displayed.
As you can see, the problem is that, if there is no error message, there will be an extra row on the page that looks out of place to the user. The code above is an overly simplied version, so the actual code has a lot more stuff in it, which prevents me from moving the <form:errors> tag inside the tag containing the field.
Is there a way to find out if there is any message associated to a given path on the JSP level? Basically, I would like to do the following:
<c:if test="${what do I write here?}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
Thanks!
You can do something like this (notice that bind is from spring taglib):
<spring:bind path = "name*">
<c:if test="${status.error}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
</spring:bind>
I solved your problem by doing this:
<table>
<form:errors path="firstName">
<tr>
<td colspan="2">
<form:errors path="firstName"/>
</td>
</tr>
</form:errors>
<tr>
<td><form:label path="firstName"><spring:message code="helloworld.label.firstName"/></form:label></td>
<td><form:input path="firstName"/></td>
</tr>
</table>
errors tag body will be evaluated only if there are errors on the path.
The simplest answer is to not use tables for page layout. Utilizing div tags alleviates this problem altogether since divs are completely dimensionless if set to hidden.

Categories