Convert Scriplet Array to JSTL Array - java

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>

Related

How can I save all the data from different textboxs in an array, using JSTL?

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>

c:forEach does not iterate all values

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;">
&nbsp &nbspZauzetost stola: &nbsp <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;">
&nbsp &nbspZauzetost stola: &nbsp <b>Zauzet </b>
</p>
</td>
</tr>
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
&nbsp &nbspDatum rezervacije: &nbsp <b> <span id="datumRez"> ${rezSto.datumRezervacije} </span> </b>
</p>
</td>
</tr>
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
&nbsp &nbspTrajanje rezervacije: &nbsp <b> ${rezSto.rezervacijaTrajanje} </b>
</p>
</td>
</tr>
<tr>
<td style="border: 0;">
<p style="font-size: 15px;">
&nbsp &nbspKorisnici:<b> &nbsp ${rezSto.gost.korisnikIme} &nbsp ${rezSto.gost.korisnikPrezime} &nbsp </b><!-- <c:forEach items="${rezSto.pozvaniPrijatelji}" var="pri"> ,<b> ${pri.gost.korisnikIme} &nbsp ${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.

Sending data using <a href="#"> in the same page and display the result (FORM) using JSP without changing the (FORM) coding

<div id="main_search">
<table>
<tr>
<td>Product</td>
<td>:</td>
<td>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral-VC">Bilateral-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral Non-VC">Bilateral Non-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget Domestic">Budget Domestic
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget High">Budget High
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget International">Budget International
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="IDD Buffet Base">IDD Buffet Base
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Premium">Premium
<br>
</td>
</tr>
<tr>
<td>Destination</td>
<td>:</td>
<td>
<input id="destination" type="text" name="destination" />
</td>
</tr>
<tr>
<td colspan="9" align="center">
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="#">SEARCH</a>
</td>
</tr>
</table>
</div>
<div id="search_result" style="overflow: auto;">
<h2>Floor Price Edit</h2>
<table id="result-tbl" class="searchTable">
<tr>
<th>Destination</th>
<th>Product</th>
</tr>
<tr>
<% String dest=r equest.getParameter( "destination"); %>
<td>
<%=d est %>
</td>
</tr>
<% if (dest !=n ull) { %>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3307/icars" user="root" password="" />
<sql:query var="dest_result" dataSource="${dbsource}">
SELECT * from icx_pricing_desk WHERE DESTINATION LIKE '
<%=d est %>%'
</sql:query>
<c:forEach var="row" items="${dest_result.rows}">
<tr>
<td>
<c:out value="${row.DESTINATION}" />
</td>
</tr>
</c:forEach>
<% } %>
<td>
<% String products[]=r equest.getParameterValues( "product"); %>
<% if(products !=n ull) { %>
<% for(int i=0; i<products.length; i++) { %>
<%=products[i]%>
<sql:query var="prod_result" dataSource="${db }">SELECT * FROM icx_pricing_desk WHERE PRODUCT = '
<%=products[i]%>'</sql:query>
<c:forEach var="row" items="${prod_result.rows}">
<tr>
<td>
<c:out value="${row.PRODUCT}" />
</td>
</tr>
</c:forEach>
<% } } %>
</td>
</table>
</div>
This is some kind of form without <form> tag and for submit button it didn't use <input type="submit"> like normal form. It use <a href="#"> to submit the data.
The question is how do you send the data using the <a href="#">???
In addition, when i click the SEARCH button both <% String dest = request.getParameter("destination"); %> and <% String products[]= request.getParameterValues("product"); %> didn't received any data.
When using the standard form, both can display result. This code is written in same page(JSP).
--The first coding cannot be changed because it was the assignment and need to solve it.--
Add a form tag in your code like
<form name="form1" method="POST">
<table>
<!-- Your code ->
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="javascript:void(0);" onclick="document.form1.submit();">SEARCH</a>
</table>
</form>
Things to remember
Form tag: method(POST), Name of form(form1)
Method called on onclick of a tag

Writing java code in thymeleaf

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

This code is not submitting to a form? Why

Since I am using get I expect to see the submitted values appended to the queryString but instead all I see is the URL of the servlet being called with nothing added at the end.
<form id="editClassList" name="editClassList" method="get" action="EditClassList">
<%
HashMap<Integer,String> classes = new HashMap<Integer,String>();
classes = (HashMap<Integer,String>) request.getAttribute("classes");
%>
<%
if(classes.size()==0){ %>
<label><input class="small-link" type="text" id="add-this-class"
size="42" value="" /></label>
<%
}
%>
<%
Set<Integer> classIds = new HashSet<Integer>();
classIds = classes.keySet();
Iterator<Integer> itr = classIds.iterator();
while(itr.hasNext()){
int nextId = (Integer)itr.next();
%>
<label><input class="small-link" type="text" id="<% out.print(nextId); %>"
size="42" value="<% out.print(classes.get(nextId)); %>" />
</label>
<img id="add-class" src="images/add.png" width="16" height="16" /><br />
<label><input class="small-link" type="text" id="class-to-add"
size="42" value="" /></label>
<%
}
%>
<label><input type="submit" id="save-class-btn" value="Save Class(es)" /></label>
</form>
A try: your input tags lacks the name attribute ?
<input name="data" class="small-link" type="text" id="class-to-add" size="42" value="" />

Categories

Resources