Below is the code I have in index.jsp using jstl 1.2.
<%# taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
<% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("getName", setName);
%>
<html>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach var="itemName" items="#{getName}" >
<tr>
<td>${itemName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
The output I was expecting is as below
Print
Hello
you
are
using
jstl
in
jsp
However below is what I am getting
Print
#{name}
Please let me know where I am missing
Below is the only jar file I have in WEB-INF/lib folder
jstl-1.2.jar
Thanks in advance
Fahim
Note: Adding Java and JSP tag as person who have knowledge of Java and JSP might be knowing JSTL too...
Here,
<%# taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
You're specifying the wrong JSTL taglib URL. This one is for JSTL 1.0. After JSTL 1.1 it requires a /jsp in the path. See also the JSTL 1.1 tag library documentation.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
As to the of the code (and to reply on all those duplicate answers complaining to use ${} instead), the #{} syntax will only work inside JSP when you're targeting a Servlet 2.5 / 2.1 compatible container with a web.xml conforming Servlet 2.5 spec. Tomcat 6.0 is an example of such a container. The #{} will indeed not work in JSP tags on older containers such as Tomcat 5.5 or older.
For clarity and to avoid confusion among starters, better use ${} all the time in JSP tags. Also better use self-documenting variable names.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String[] names = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("names", names);
%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JSTL demo</title>
</head>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach items="${names}" var="name">
<tr><td>${name}</td></tr>
</c:forEach>
</table>
</body>
</html>
See also:
Our JSTL wiki page
Difference between JSP EL, JSF EL and Unified EL
In JSTL 1.2, you don't want to use #{name} in pure JSP, that's only a JSF artifact. Instead, simply use ${name}.
You need to refer items using expression language like ${name}
U r using # instead of $ before name
Let me know if this resolves.
#{name} is not a valid Java variable reference - looks like you are confusing it with JQuery selector.
Anyways try just using items="${name}"
#{name} is should be like ${name}
oh! might be the jars related to JSTL. check thins link for those jars to include in your project
Below is the final code I am using and it is running...
Posting so that someone can use it... Might help me tomorrow ;)
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("getName", setName);
%>
<html>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach var="itemName" items="#{getName}">
<tr>
<td>${itemName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Learning : I was using <%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %> instead of <%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Related
I want to print out every Item of my list "sorts" with Expression Language in a JSP File like that:
Try: Pizza-Margherita
Try: Cheese-Pizza
So it works if i use a normal expression like this
Try: ${sorts[0]}
Try: ${sorts[1]}
But i have to write it for every Item in the List
So I tried to use following two Loops:
<c:forEach items="${sorts}" var="item">
Try: ${item}<br>
</c:forEach>
<c:forEach var="item" items="${sorts}">
<td>
Try: <c:out value="${item}" />
</td>
</c:forEach>
It didn't work and I got this output each time:
Try:
Why won't my foreach loop work? what have I done wrong?
It is because you haven't included the core tag library in your JSP file.
You will do this by inserting following Line at the top of your file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Here is sample JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<c:forEach var="item" items="${sorts}">
${item.name}
</c:forEach>
</body>
</html>
Here is Sample Java Code
List<Sort> sortList = new ArrayList<>();
Sort s1 = new Sort();
s1.setName("Pizza-Margherita");
Sort s2 = new Sort();
s2.setName("Cheese-Pizza");
sortList.add(s1);
sortList.add(s2);
request.setAttribute("sorts", sortList);
Sample Object class
public class Sort {
private String name;
//create getter and setter for name
}
Make sure you have imported JSTL Library.
I have a plugin for Openfire. There is a JSP page inside it, and I want the locale on this page to be different from the global locale.
I tried this:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
String locale = ParamUtils.getParameter(request, "locale");
%>
<html>
<head>
<title>Page</title>
</head>
<body>
<fmt:setLocale value="<%=locale%>" scope="session"/>
<fmt:message key="hello.world"/> <br>
</body>
</html>
But it doesn't work.
When I switch the global locale, my JSP page shows me right language. But I don't want to change the global locale, I only want to change it for this page.
What can I do to make this work?
Good day all,
I saw <html:html></html:html> from a jsp page in a java project.
Would like to ask what is the difference between these html tags.
Kindly advise.
The example code is as follow:
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<head>
</head>
<body>
<!-- some html code here -->
</body>
</html:html>
<html:html> uses the struts-html tag library, where <html></html> is just plain old html.
You can read all about the struts-html taglib here.
both are same.html:html is struts 1 tag which is equal to basic HTML's html tag.
my model returns an arraylist of strings to servlet in the form
ArrayList<String> currentCustomer = model.getAllCustomers();
i want to pass this arraylist from the servlet to the jsp page. how do i do this? below is what i tried
req.setAttribute("currentCustomer", currentCustomer);
and in the jsp page, i want to use JSTL to loop over each value and display it. how do i do that? its frustrating me to no end. ive scoured the web but to no avail. any help is greatly appreciated.
here is the jsp code
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<body>
<div>
<c:forEach var="customer" items="currentCustomer">
${customer}
</c:forEach>
</div>
</body>
Let's make it work :)
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach var="customer" items="${currentCustomer}">
<c:out value="${customer.name}" />
<c:out value="${customer.age}" />
</c:forEach>
P.S. jsp:useBean is another way to go...
P.P.S. I also made a correction in the taglib import. That's one of these hard-visible mistakes when you can look on two different entries and think they are the same :)
its allrite guys, i solved the problem.. thanks for your help..
apparently the code i was using was outdated (thanks internet!) i was writing this on the header:
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
while it should have been
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
It will be smt like
<c:forEach var="currentCustomer" items="${customers}">
${currentCustomer.name}
${currentCustomer.age}
</c:forEach>
i have the following JSP:
<%# page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%# page isELIgnored="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><c:out value="${it.title}"/></title>
</head>
<body>
<c:forEach var="speaker" items="${it.speakers}" varStatus="stat">
<ul>
<li><c:out value="${speaker.person.firstName}" /> <c:out value="${speaker.person.lastName}" />, <c:out value="${speaker.person.address.city.zip}" /> <c:out value="${speaker.person.address.city.name}" /></li>
</ul>
</c:forEach>
</body>
</html>
Eclipse warns me about every instance of EL Expressions in my code:
Warning [line 10]: "value" does not support runtime expressions
Warning [line 13]: "items" does not support runtime expressions
...
this is however not true, EL gets evaluated correctly by the server.
Can anyone hint me in the right direction why eclipse is warning me about those EL expressions?
Your taglib directive imports a JSTL 1.0 taglib. It should be JSTL 1.1 instead (note the difference in URI):
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Possible solution (found here):
Twin Libraries
The JSTL tag libraries come in two
versions which differ only in the way
they support the use of runtime
expressions for attribute values.
In the JSTL-RT tag library,
expressions are specified in the
page's scripting language. This is
exactly how things currently work in
current tag libraries.
In the JSTL-EL tag library,
expressions are specified in the JSTL
expression language. An expression is
a String literal in the syntax of the
EL.
When using the EL tag library you
cannot pass a scripting language
expression for the value of an
attribute. This rule makes it possible
to validate the syntax of an
expression at translation time.
So maybe your eclipse and the server use different tag libraries.
try this:
change this:
<%#taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
to yes:
<%#taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
hope it works for you. I got this from www.csdn.net.