How to convert java code to jstl - jstl

I cannot found the way to convert java code to jstl
This is my java code
for(int i=0;i<listCategory.size();i+=3){
int size = i+3<listCategory.size() ? i+3 : listCategory.size
for(int j=i;j<size;j++){
Category cate = lisCategory.get(j);
}
}
Please help me :(

Hi I’m not sure if I understand what you want, but ill try to help you if I can, The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. Below is the tag for the core libary.
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
If you want to emulate your for loop from Java you would use the <c:forEach> Tag. This will allow you to iterate over a list in your JSP page assuming you include the taglib provided above. Here is an example for you to view.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:forEach> Tag Example</title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>
I hope this gives you some insight or helps you along your journey.

Related

Image source unknown when url is returned from controller

I am new to Ui design and I am trying to write simple application using JSP and spring boot.
The idea is to get the Google QR code URL in the controller and return to JSP page:
https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FSpringRegistration%3Atest_user%3Fsecret%3DI2S5OTJXDG5NBWVY%26issuer%3DSpringRegistration
So, my controller looks like as below:
#GetMapping("/confirmRegistration")
public String confirmRegistration(final HttpServletRequest request,
final Model model) {
final User user = userService.getUser(token);
String qrUrl = userService.generateQRUrl(user);
log.info("QR URL: {}", qrUrl);
model.addAttribute("qr_code", qrUrl);
return "redirect:/qrcode";
}
My JSP looks like -
<!DOCTYPE html>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
<title>Confirm your account</title>
</head>
<body>
<label>Scan following QR code.</label>
<c:if test="${param.url != null}">
<span> hi there </span>
${param.url}
</c:if>
<c:url value="${qr_code}" />
<img src="${qr_code}" width=256 height=256 />
<br />
<img src="${param.qr[0]}"/>
</body>
</html>
But it is not populating the image. UI page looks like below:
Can someone please help me here?
I think, the issue is related to return statement. You should point a name of your jsp page that the model with image url is forwarded to. But before, you need to define and configure InternalResourceViewResolver
For more info (redirecting / forwarding)

Value not displayed into Spring MVC view

I'm having a problem with displaying a string in jsp view. A a web framework I'm using Spring MVC.
The view (return.jsp) is like that:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FredomWings</title>
</head>
<body>
<b>Result -></b>
<p>${currentValue}</p>
</body>
</html>
The controller looks like that:
#RequestMapping(value="balance")
public String getBalance(Model model) {
model.addAttribute("currentValue","alalal");
return "return";
}
What is the reason for which in the jsp page only Result -> without any value is displayed?

Display a list by display:table tag in Spring + Hibernate MVC project

I am doing on a MVC project using netbean IDE and I have a problem with displaying my objects on table
This is my jsp page
<%#page import="java.util.List"%>
<%#page import="java.util.ArrayList"%>
<%#page import="model.Clothes"%>
<%#page import="org.hibernate.Session"%>
<%#page import="cfg.HibernateUtil"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="display" uri="http://displaytag.sf.net" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Clothes</title>
</head>
<body>
<div style="margin-left: 10%; ">
<%
Session session1 = HibernateUtil.getSessionFactory().getCurrentSession();
List<Clothes> clothes = new ArrayList<Clothes>();
session1.beginTransaction();
clothes = session1.createQuery("from Clothes").list();
session1.getTransaction();
System.out.println(clothes.size() + "aaaaaaaaa");
session1.close();
request.setAttribute("results", clothes);
%>
<display:table name="results" pagesize="10"/>
</div>
<jsp:include page="/index.htm" flush="true"/>
</body>
And the browser display an exception at line contains 'display' tag like this:"org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/clothes.jsp" Where did I do wrong, please help me, I am newbie in java web.
p/s: I add libs in my project, they are: displaytag-1.2, displaytag-export-poi-1.2, displaytag-portlet-1.2 and commons-lang-2.6.
I followed #JB Nizet instruction and it worked, just use the libs exactly the same that listed in this link: http://www.displaytag.org/1.2/displaytag/dependencies.html
However, in some PC, when using org.slf4j, your project can not work properly, just remove them from the project, it is OK!

how to implement optiontransferselect using ajax

I'm trying to implement optiontransferselect using ajax. I can not move the values from left side to right side.I've added <%# taglib uri="/struts-dojo-tags" prefix="sx"%> tags in my code and I've included <sx:head/> also, but it doesn't work. please help me out to solve the issue.
my.jsp
<%#taglib uri="/struts-tags" prefix="s" %>
<%# taglib uri="/struts-dojo-tags" prefix="sx"%>
<head>
<sx:head/>
</head>
<div class="width2 margin-bottom margin-top">
<div class="profile-text">
Preferred Location
</div>
<s:optiontransferselect
id="city"
name="cityNo"
list="cityList"
listKey="id"
listValue="cityName"
headerKey="0"
headerValue="City"
onfocus="getcitywithCountry(false);"
doubleList="prefererList"
doubleId="city"
doubleListKey="cityId.id"
doubleListValue="cityId.cityName"
doubleHeaderKey="0"
doubleHeaderValue="--selected locations--"
doubleName="selectedLocations" allowUpDownOnLeft="false" allowUpDownOnRight="false"/>
</div>
I don't see much wrong with your code. You need to remove <%# taglib uri="/struts-dojo-tags" prefix="sx"%> reference. Replace <sx:head/> with <s:head/>.

What's the best way to deliver both static and AJAX page content?

What is the best-practice for developing web-pages for both non-JS-enabled and JS-enabled pages? I am developing a Spring MVC web-application using JSP/JSTL as the view technology.
The way I work is to create a full web-page (with "html", "head", etc) tags in it. This will work on all browsers. My entire app works (however ugly-bugly) with JS disabled.
Also included in each page is some jQuery script that prettifies the page, typically turning top-level "divs" into tabs, other divs into dialogs, etc. The JS "hijacks" the underlying HTML. My JS hijacks the links and buttons, using AJAX calls to load the content into the correct dialog or tab div.
This all works nicely and I like the architecture, but I have added an optimisation such that the AJAX requests appends a "contentOnly" parameter; this is picked up by the Spring MVC view which conditionally ignores the "head", etc - i.e. it only renders the real content that the AJAX version wants. It just feels clunky.
I know I could load the entire page and discard the outer bits but this seems inefficient to load all the associated CSS and JS files which are not strictly necessary.
My question is: is there a nicer way to write this conditional formatting, should I be using a different view technology or something else, something like Velocity or FreeMarker or whatever Grails uses?
An example of my conditional: -
<%# 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" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:if test="${param.contentOnly == null}">
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
</c:if>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
<c:if test="${param.contentOnly == null}">
<%# include file="_footer.jsp"%>
</body>
</html>
</c:if>
You need to include the files the other way round.
/WEB-INF/template.jsp
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
<jsp:include page="${page}.jsp" />
<%# include file="_footer.jsp"%>
</body>
</html>
leads.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
And create a controller which sets leads as ${page} variable based on a specific request URL and forward to template.jsp instead. Since I don't do Spring, I can't answer in detail how to achieve it with Spring. But the following basic JSP/Servlet kickoff example should give the general idea:
#WebServlet(urlPatterns={"/pages/*"})
public class TemplateServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page = request.getPathInfo();
// .. Do some other preprocessing if necessary .. and then:
request.setAttribute("page", page);
request.getRequestDispatcher("/WEB-INF/template.jsp").forward(request, response);
}
}
Invoke it as http://example.com/contextname/pages/leads to get the template displayed with leads.jsp included. Finally you should be able to invoke http://example.com/contextname/leads.jsp independently to get the sole template content.
I have used that kind of approach a few years ago with Freemarker and I don't know better way to do it.
What you could do is to create JSP-tags for those conditions so that they look nicer and hides the actual implementation. Something like this:
<t:NonAjax>
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
</t:NonAjax>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
<t:NonAjax>
<%# include file="_footer.jsp"%>
</body>
</html>
</t:NonAjax>

Resources