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 fetch data from database in spring mvc, this is my code for fetching data
#Override
public List getOrderDetail(Integer shop_id) {
// TODO Auto-generated method stub
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(
"SELECT order_id,shop_id,delivery_address,total_amount FROM master_order WHERE shop_id =" + shop_id);
SQLQuery query1 = sessionFactory.getCurrentSession().createSQLQuery(
"SELECT itm.image,itm.name,itm.brand,odr.quantity,itm.offerprice,(itm.offerprice*odr.quantity) AS Total,modr.cust_id FROM master_order modr\r\n"
+ "INNER JOIN tblorder odr ON modr.order_id = odr.order_id INNER JOIN item itm on odr.item_id = itm.item_id WHERE modr.shop_id ="+shop_id);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
query1.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List results = query.list();
List results1 = query1.list();
results.add(results1);
System.out.println(results);
return results;
}
above System.out.println(results); statement give following output
[{shop_id=1, delivery_address=1, abc, xyz, aaa, USA, order_id=6, total_amount=1800}, [{image=Screenshot (19).png, quantity=8, Total=800, name=DhruvRajkotiya, offerprice=100, brand=gsgsrgs, cust_id=2}, {image=Screenshot (19).png, quantity=10, Total=1000, name=qwe, offerprice=100, brand=asdf, cust_id=2}, {image=Screenshot (19).png, quantity=1, Total=100, name=ewq, offerprice=100, brand=gsgsrgs, cust_id=2}]]
i want print order_id as well as {image=Screenshot (19).png, quantity=10, Total=1000, name=qwe, offerprice=100, brand=asdf, cust_id=2} in jsp page
this is my controller code
#RequestMapping(value = "/notifications", method = RequestMethod.GET)
public String notifications(Model model, HttpServletRequest request) {
if (request.getSession().getAttribute("shopkeeper") == null) {
return "redirect:login";
} else {
Shopkeeper sp = (Shopkeeper) request.getSession().getAttribute("shopkeeper");
model.addAttribute("order", shopkeeperService_shopkeeper.getOrderDetail(sp.getSk_id()));
return "shopkeeper/notifications";
}
}
this is my jsp code
<div class="row">
<c:forEach items="${ order}" var="order">
<div class="col-lg-12">
<div class="card">
<div class="card-header" style="padding-bottom: 5px;">
<h6># ${order.shop_id }</h6>
</div>
<div class="card-body">
<div class="col-lg-2" style="float: left;">Image</div>
<div class="col-lg-2" style="float: left;">Product name</div>
<div class="col-lg-2" style="float: left;">Product brand
</div>
<div class="col-lg-1" style="float: left;">Quantity</div>
<div class="col-lg-1" style="float: left;">Unit Price</div>
<div class="col-lg-1" style="float: left;">Total</div>
<div class="col-lg-3" style="float: left;">Approve of
request</div>
</div>
<div class="card-body">
<div class="col-lg-6" style="float: left;">Delivery
Address</div>
<div class="col-lg-6" style="float: left;">Total Amount</div>
</div>
</div>
</div>
</c:forEach>
</div>
i want to print
my question concept is order_id,address,total_amount one time and multiple bought item that can be one or more
how i can print
The way to "send" some data to a JSP page is to set and pass a request (or session) attribute.
From a Servlet, which handles current request, in a doGet() or doPost() method (depending on the HTTP request method used), you can do:
request.setAttribute("attributeName", attributeObject);
request.getRequestDispatcher("<name_of_your_page>.jsp").forward(request, response);
where the attributeName is a string, which will be treated as a reference to the attributeObject on a JSP page.
Then on the JSP page, you can use JSTL library and Expression Language (see this tutorial regarding JSTL and this one regarding JSP & EL in general). To iterate over the list, which has been passed as a request attribute, use <c:forEach> tag from JSTL.
First of all you need to import the core JSTL tags by placing this line on the top of your JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Another useful statement to put under the latter is:
<%# page isELIgnored="false" %>
which tells your web browser to evalue the expressions written in EL instead of including them in HTML body as a plain text.
Provided that you used the string "orders" to name the collection set as attribute, the <c:forEach> tag usage can look similarly as below. You'll probably want to structure it differently and the field names (like order.order_id) can be different in your case, but I think you can get the idea.
<c:forEach item="order" items="${requestScope.orders}">
<p>${order.order_id}</p>
<p>${order.delivery_address}</p>
...
</c:forEach>
You can save the list as a model attribute. For example you want to save the current user:
model.addAttribute("user",user);
and access this attribute in your jsp file inside a div maybe :
<div> ${user.name} </div>
The controller method could look like this:
#RequestMapping( value = "/user", method = RequestMethod.GET )
public String displayUser(final Model model)
{
final User user = userService.findUserById(1); // here you'll provide custom implementation
model.addAttribute("user",user);
return "somejsppage.jsp";
}
If you want to add a List to your model:
final List<User> users = fillUserList(); // your implementation here
model.addAttribute("users",users);
You can access every element in that list via <c:forEach> tag:
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>
<table>
<tr>
<th>First Name</th>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.firstName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
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>
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
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>