In Short
I was tring to create a tag for pagination in thymleaf.
In Detail
I already have an example for this in jsp.but I am stuck in the middle.I am not sure how to write this code in thymleaf.
I googled about it,but the results were very confusing.
Example jsp :
<%#tag description="Extended input tag to allow for sophisticated errors" pageEncoding="UTF-8" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#tag import="net.megalytics.util.Href" %>
<%#attribute name="currentPage" required="true" type="java.lang.Integer" %>
<%#attribute name="totalPages" required="true" type="java.lang.Integer" %>
<%#attribute name="totalItems" required="true" type="java.lang.Long" %>
<%
if (totalPages > 1) {
String currentUrl = request.getAttribute("javax.servlet.forward.request_uri").toString();
String queryString = "";
if (request.getQueryString() != null)
queryString = request.getQueryString();
Href newUrl = new Href(currentUrl + "?" + queryString);
newUrl.addParameter("page", String.valueOf(currentPage));
String url = "";
Integer totCount =0;
%>
<div class="pull-right">
<ul class="pagination">
<c:choose>
<c:when test="<%=currentPage == 0%>">
<li class="disabled">First</li>
</c:when>
<c:otherwise>
<li class="">
<%
newUrl.removeParameter("page");
newUrl.addParameter("page", "0");
url = newUrl.toString();
%>
First
</li>
</c:otherwise>
</c:choose>
<c:forEach var="count" step="1" begin="1" end="<%= totalPages %>">
<c:choose>
<c:when test="${(currentPage == count-1)}">
<li class="disabled">${count}</li>
</c:when>
<c:otherwise>
<li>
<%
newUrl.removeParameter("page");
newUrl.addParameter("page", String.valueOf(totCount));
url = newUrl.toString();
%>
${count}
</li>
</c:otherwise>
</c:choose>
<%
totCount++;
%>
</c:forEach>
<c:choose>
<c:when test="<%=currentPage == (totalPages-1) %>">
<li class="disabled">Last</li>
</c:when>
<c:otherwise>
<li class="">
<%
newUrl.removeParameter("page");
newUrl.addParameter("page", String.valueOf(totalPages - 1));
url = newUrl.toString();
%>
Last
</li>
</c:otherwise>
</c:choose>
</ul>
</div>
<%
}
%>
Can anyone help me? I am stuck...
Thymeleaf or neither any framework doesn't encourage you to write logic inside your views. This is poor coding practice.
You can do the following instead.
Create a bean method with the logic below
#Bean("urlUtil")
class UrlUtil {
String doSomthing() {
newUrl.removeParameter("page");
newUrl.addParameter("page", "0");
return newUrl.toString();
}
}
Access the bean inside the thymeleaf layout
<a th:href="#{__${#urlUtil.doSomthing()}__}">First</a>
Refer http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#the-springstandard-dialect
Related
I want to recieve any number of textbox values, and save it to an array with JSTL if it's possible.
I generate all the texbox where numberAsked can be any number.
<c:if test="${param.buttonSend != null}">
<form action="index.jsp" method="post">
<c:forEach var = "i" begin = "1" end = "${param.numberAsked}">
<input type="text" name="textbox[]" class="form-control">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
</c:if>
Now I want to save all the textboxs in an array.
<c:if test="${param.buttonSave != null}">
<c:set var="data" value="${request.getParameterValues(textbox)}"/>
<c:forEach var = "item" items="${param.data}">
<c:out value="${item}"/>
</c:forEach>
</c:if>
But, it doesn't work, how can I save all the data from all the generated textboxs in an array?.
Here is a demonstration JSP.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<form method="post">
<c:forEach var = "i" begin = "0" end = "5">
<input type="text" name="textbox">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
<c:set var="data" value="${paramValues.textbox}"/>
<c:forEach var = "item" items="${data}">
${item}
</c:forEach>
I am trying to add products (in viewProduct.jsp) to a basket (basket.jsp).
I'm pretty new to JSP. I know it's done by using an appropriate hyperlink to basket.jsp in viewProduct.jsp. I've written a long line in <p> tags in viewProduct.jsp where I'd like the hyperlink to be. Thanks
viewProduct.jsp
<%# page import="shop.Product"%>
<jsp:useBean id='db'
scope='session'
class='shop.ShopDB' />
<html>
<head>
<title>My Shop</title>
</head>
<body>
<%
String pid = request.getParameter("pid");
Product product = db.getProduct(pid);
// out.println("pid = " + pid);
if (product == null) {
// do something sensible!!!
out.println( product );
}
else {
%>
<div align="center">
<h2> <%= product.title %> by <%= product.artist %> </h2>
<img src="<%= product.fullimage %>" />
<p> <%= product.description %> </p>
<p> -------------------------- link goes here -------------------</p>
</div>
<%
}
%>
</body>
</html>
basket.jsp
<%# page import="java.util.Collection,
java.util.Iterator"%>
<jsp:useBean id='basket'
scope='session'
class='shop.Basket'
/>
<%
String empty = request.getParameter("emptyBasket");
if (empty!=null) {
basket.clearBasket();
}
String item = request.getParameter("addItem");
basket.addItem(item);
%>
<html>
<body>
<% Collection items = basket.getItems();
for (Iterator i = items.iterator(); i.hasNext(); ) {
out.println( "<p>" + i.next() + "</p>" );
}
%>
<p> Order total = <%= basket.getTotalString() %>
<%
if ( basket.getTotal() > 0) {
%>
<form action="order.jsp" method="post">
<input type="text" name="name" size="20">
<input type="submit" value="Place Order" />
</form>
<form action="basket.jsp" method="get">
<input type="hidden" name="emptyBasket" value="yes">
<input type="submit" value="Empty Basket" />
</form>
<%
}
%>
</body>
</html>
As you can see from your basket.jsp if you have a parameter called addItem, then it will be added to the basket
so how about
<p> <%= product.description %> </p>
<!-- use pid -->
<a href="basket.jsp?additem="<%=pid%>><%= product.description %></a>
Although use of JSTL tags would be nicer.
See http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
I have a arraylist tagged to a session, and when i try to iterate with c:forEach, it shows only first two class and there attributes, and when it comes to third class element in that arraylist, it just doesnt show values of attributes.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<table id="myTable">
<c:choose>
<c:when test="${jednaRez.stoRez.zauzetostStola == false || nema == true}">
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
   Zauzetost stola:   <b>Slobodan </b>
</p>
</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${rezeSto}" var="rezSto">
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
   Zauzetost stola:   <b>Zauzet </b>
</p>
</td>
</tr>
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
   Datum rezervacije:   <b> <span id="datumRez"> ${rezSto.datumRezervacije} </span> </b>
</p>
</td>
</tr>
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
   Trajanje rezervacije:   <b> ${rezSto.rezervacijaTrajanje} </b>
</p>
</td>
</tr>
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
   Korisnici:<b>   ${rezSto.gost.korisnikIme}   ${rezSto.gost.korisnikPrezime}   </b><!-- <c:forEach items="${rezSto.pozvaniPrijatelji}" var="pri"> ,<b> ${pri.gost.korisnikIme}   ${pri.gost.korisnikPrezime} </b> , </c:forEach>--> <br/>
____________________________________________________________
</p>
</td>
</tr>
<br/>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
Any ideas?
This is servlet where i create arrayList rezeSto:
Korisnik men = (Korisnik) request.getSession().getAttribute("menadzer");
ArrayList<Rezervacija> sveRez = new ArrayList<Rezervacija>(rezervacijaDao.rezRestoran(men.getRestoran().getId()));
ArrayList<Rezervacija> vazeceRez = null;
try {
vazeceRez = new ArrayList<Rezervacija>((List<Rezervacija>)men.vazeceRez(sveRez));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayList<Rezervacija> rezStoIsti = new ArrayList<Rezervacija>();
//samo one rezervacije koje sadrze poslati sto
for(int i=0; i<vazeceRez.size(); i++)
{
if(vazeceRez.get(i).getStoRez().getId().equals(sifraStola))
{
rezStoIsti.add(vazeceRez.get(i));
}
}
request.getSession().setAttribute("nema", false);
if(rezStoIsti.size() == 0)
{
request.getSession().setAttribute("nema", true);
}
else
{
request.getSession().setAttribute("jednaRez", rezStoIsti.get(0));
request.getSession().setAttribute("rezeSto", rezStoIsti);
}
request.getRequestDispatcher("stoPrikaz.jsp").forward(request, response);
return;
If c:forEach prints first two and and you don not see the other list elements, i would suppose that when the second is got printed it breaks your html, that's why you don't see it, try to debug maybe really some special character gets printed out.
I already have posted this post yesterday..but nobody answerd it..
I am using spring mvc framework.
I want to do pagination like the below picture:--
So I have done :--
#RequestMapping(value = "/detailssection/id",method=RequestMethod.GET)
public #ResponseBody String showthedetails(Map<String, Object> map, HttpServletRequest request) throws IOException{
//I want to display 5 details per page
int recordsPerPage = 5;
//It will count total no of records(like 300 records are there)
int totalnoOfrecords = viewService.TotalnoOfRecoredsbanglains1();
//If the totalnoOfrecords=300 then page noumber will be 300/5=60 that means 1,2....60
int pagenumbers = (int) Math.ceil(totalnoofrecords * 1.0 / recordsPerPage);
map.put("detail", new Detail());
map.put("noOfPages", pagenumbers);
map.put("detailList", viewService.viewDetails());
return "detailssection";
}
and my jsp page is like:--
<div id= "part1">
<div id= "details">
<p>${detail.description}</p>
</div>
</c:forEach>
<%--For displaying Previous link except for the 1st page --%>
<c:if test="${currentPage != 1}">
Previous
</c:if>
<%--For displaying Page numbers.
The when condition does not display a link for the current page--%>
<c:forEach begin="1" end="${noOfPages}" var="i">
<c:choose>
<c:when test="${currentPage eq i}">
${i}
</c:when>
<c:otherwise>
${i}
</c:otherwise>
</c:choose>
</c:forEach>
<%--For displaying Next link --%>
<c:if test="${currentPage lt noOfPages}">
Next
</c:if>
</div>
But I am not getting any page numbers.its only showing the "previous page" section.Its like:--
what am I doing wrong?Is there any problem in jsp page??
Let's start from the begining.
If you have a map on your requestScope, suppose it is MyMap with the following keys: details, noOfPages and detailList, to access it's values by keys, the syntax is the following:
<c:forEach begin="1" end="$MyMap['noOfPages']" var="i" varStatus="loop">
<c:choose>
<c:when test="${currentPage eq i}">
${i}
</c:when>
<c:otherwise>
${loop.index}
</c:otherwise>
</c:choose>
</c:forEach>
Hello I'm trying to change this bit of code into JSTL. I'm using scriptlets here.
<div id="slider">
<form action="gamepage" method="post">
<div class="sliderclass" id="slider1">
<% SiteDatabase database = new SiteDatabase();
Game game =(Game) database.getGames();%>
<% String[] newgames = game.getNewGames();
String[] imgSrc = {"images/spidermancover.jpg","images/blackopscover.jpg", "images/laststorycover.jpg", "images/ncaa13cover.jpg"
,"images/uncharted3cover.jpg", "images/mariokart7cover.jpg"};%>
<%for(int i =0; i < 6; i++) {%>
<div class="contentwrapper">
<button type="submit" style="border: 0; background: transparent" name="game" VALUE="<%=i%>"><img src="<%=imgSrc[i]%>" name="what"/></button><div id="slidertext"><%= newgames[i] %></div></div>
<%}%>
</div>
</form>
</div>
to something like this
<div class="slider">
<div id="slider1" class="sliderclass">
<jsp:useBean id="games" class="services.SiteDatabase" scope="request">
<jsp:SetProperty property="gameTitle" name="games" value="gameTitle"/>
</jsp:useBean>
<jsp:useBean id="game" class="services.Game" scope="request">
</jsp:useBean>
<c:set var="gameTitle" scope="request" value="game"/>
<c:set var="gameSrc" scope="request"><jsp:getProperty property="gameSrc" name="game"/></c:set>
<c:set var="gameId" scope="request"><jsp:getProperty property="gameId" name="game"/></c:set>
<c:forEach var="i" begin="0" end="6">
<div id="contentwrapper">
<img src="" name="what" />
<div id="slidertext"><jsp:getProperty property="gameTitle" name="gameTitle" /></div>
</div>
</c:forEach>
</div>
</div>
The scriplet works fine, i just want to meet standards and use JSTL
You can iterate array/list via JSTL <c:forEach/>.
<c:forEach var="img" items="images/spidermancover.jpg","images/blackopscover.jpg",
"images/laststorycover.jpg", "images/ncaa13cover.jpg",
"images/uncharted3cover.jpg","images/mariokart7cover.jpg">
<p>
${img}
</p>
</c:forEach>
Or
<c:forEach var="game" items="${games.newGames}">
<p>
${game}
</p>
</c:forEach>