aspose file tools*
The moose likes JSP and the fly likes Can't get first JSTL to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Can Watch "Can New topic
Author

Can't get first JSTL to work

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Hi,

I have JSTL set up in MyEclipse for the current project. When I "deploy" the project, MyEclipse copies the jstl.jar and standard.jar and all the .tld files to the Webapps folder.

However, when I try to run the code, the "$" don't expand to show me what's in the variables. Clearly, I've got something set up wrong.

Below, I have my source code, the URL I used in the browser, and the browser's output.

I would appreciate any help or suggestions why this isn't working.

Here's my test JSP page:

<%@ page language="java" import="java.util.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c'%>

This is my JSP page.

<%
String s = "JSTL is cool.";
pageContext.setAttribute("name", s);
%>

Getting ready to do some JSTL ...
<br>
<c:if test ='$(not empty param.amount)'>

<c ut value='$(param.amount)' />
<c ut value='$(name)' />

</c:if>
Okay, here's the value of name...
<br>
<c ut value='$(name)' />


Here's the value of param.amount
<br>
<c ut value='$(param.amount)' />

</body>
</html>
======================================

URL line in browser: http://localhost/JSTLTest/MyJsp.jsp?amount=100

Here's the output with the above URL line:

This is my JSP page. Getting ready to do some JSTL ...
Okay, here's the value of name...
$(name) Here's the value of param.amount
$(param.amount)

===================================

Look forward to any replies or suggestions why this isn't working.

Thanks.

-- Mike
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
<c:if test ='$(not empty param.amount)'>

replace the () (round braces) with {} (curly braces) in all instances of JSTL tags.


42
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Ok, thanks.

Now, when I replace the () with {}, I get the following error:

org.apache.jasper.JasperException: /MyJsp.jsp(38,0) According to TLD or attribute directive in tag file, attribute test does not accept any expressions

----

I'm using the 1.1 version of the JSTL.

Any ideas what's going on?

Thanks again!!!

-- Mike
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
what about including the correct JSTL declaration in your JSP? Just noticed that's missing as well...
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Hi,

I'm not sure what you mean.

If:

<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c'%>

isn't correct, what am I missing?

Thanks.

-- Mike
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
First try using double quotes, it may not matter.

Also try the following:



[Bear edit: consolidated multiple replies. Craig, please make corrections by editing your original reply rather than making multiple replies with corrections.]
[ April 12, 2005: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67


If:

<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c'%>

isn't correct, what am I missing?


That is the URI for the JSTL 1.0. You stated you are using JSTL 1.1. The URI for that would be:



You are using a JSP 2.0 capable container, right?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

The JSTL 1.1 Specification is available here.

If you are writing JSTL you should have a copy on your desk (or desktop). The proper URI's are on page 2.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
This has no effect.

"Core JSTL" uses single quotes.

I still get the same error.

Not sure what's going on with this error:

org.apache.jasper.JasperException: /MyJsp.jsp(38,0) According to TLD or attribute directive in tag file, attribute test does not accept any expressions

Thanks again in advance!!!

-- Mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
The problem with my code was I needed a:

<%@ page isELIgnored="true" %>

at the top of the page.

The issues are these:

1) You are not allowed to use JSP scripting elements for "test". Ie the following is illegal:
<c:if test="<%= Math.random() < 0.5 %>">

2) You are using Tomcat 5 and a servlet 2.4 declaration in your web.xml, so the JSP 2.0 expression language is enabled.

3) The ${...} is interpreted by JSP 2 before JSTL gets a chance to get a crack at it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

"Core JSTL" uses single quotes.


In any tag library you can use single or double quotes. It makes no difference.

If you are using JSP 2.0, you should be using JSTL 1.1 precisely because in JSP 2.0 it is the container that manages the EL evaluation rather than the JSTL itself.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Thanks for your reply.

I am using JSTL 1.1, but apparently still needed the

<%@ page isELIgnored="true" %> at the top of the JSP file or get the error I posted.

The single vs. double quotes comment I made was just that it didn't affect anything relative to my problem. So I agree with your comment there.

Thanks again.

-- M
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

The question is, if you are using JSP 2.0 and JSTL 1.1, why are you using a scripting expression at all? You should be using the EL on the page, not disabling it.
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
Excellent Mike thank you for posting your solution. I have the same setup and the exact same problem, the isELIgnored tags works a treat. Nice one.

cheers
Martin

Originally posted by Mike London:
The problem with my code was I needed a:

<%@ page isELIgnored="true" %>

at the top of the page.

The issues are these:

1) You are not allowed to use JSP scripting elements for "test". Ie the following is illegal:
<c:if test="<%= Math.random() < 0.5 %>">

2) You are using Tomcat 5 and a servlet 2.4 declaration in your web.xml, so the JSP 2.0 expression language is enabled.

3) The ${...} is interpreted by JSP 2 before JSTL gets a chance to get a crack at it.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
No no no no NO!

You do NOT need: <%@ page isELIgnored="true" %>
You need to replace
this: <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c'%>
with: <%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>


Mikes solution is WRONG! You should NOT be disabling EL evaluation on the page.
Ok, it does make it work, but there is a better approach.
USE THE CORRECT JSTL URI!!!

It is frustrating to me that
- there is an FAQ on this subject (that obviously noone reads)
- this question gets asked at least once every couple of days
- Despite the numerous correct answers given, people can still come up with the wrong solution, and then other people USE THAT INCORRECT SOLUTION!

(pant...pant...pant...)
Ok, I'm calm now..... really.

If you have a JSP1.2 container (Tomcat 4), use JSTL1.0: <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
If you have a JSP2.0 container (Tomcat 5), user JSTL1.1: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Note the subtle difference with the addition of "/jsp" in there?
JSTL1.0 has to evaluate the expression itself, and so you can't use runtime expression values for that taglib.
JSTL1.1 expects the container to handle the expression - so it accepts runtime expressions.
[ August 01, 2006: Message edited by: Stefan Evans ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Originally posted by Stefan Evans:
No no no no NO!


What he said.

Turning off the EL is putting a band-aid on chicken wire held up with bubble gum.

Fix the configuration problem, don't cobble together a patch for the symptoms.
[ August 01, 2006: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get first JSTL to work