aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes help-standard tag c:forEach practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "help-standard tag c:forEach practice" Watch "help-standard tag c:forEach practice" New topic
Author

help-standard tag c:forEach practice

Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
Hello,

I try to practice the c:forEach. But I got errors. Can anybody tell me what's wrong with my code?

Servlet code:
****************************************
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class JstlTest extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String[] movieList={"Gone with wind","Road home", "tiger", "good bye"};
request.setAttribute("movieList", movieList);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
}
********************************************
JSP code:
----------------------------------------------------------------
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><body>
<strong>movie list:</strong>
<br><br>

<table>
<c:forEach var="movie" item="${movieList}">
<tr>
<td>${movie} </td>
</tr>
</c:forEach>
</table>
</body></html>
-------------------------------------------------------------
I put jstl.jar in the WEB-INF/lib/

when I called servlet I got the error:

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:114)
org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:316)
org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:147)
org.apache.jasper.compiler.Parser.parseTaglibDirective

So what's wrong? Do I need tld file? How can I let this program run?

Thanks a lot.

Anna
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

It's not complaining about the code you posted. It's complaining that the uri for the core taglib is not mapped in your web.xml file.

However, you can avoid the need to map the taglib in the web.xml by declaring a uri attribute in the directive that references your actual tld file. The appropriate uri might look something like this: /WEB-INF/c.tld


A good workman is known by his tools.
Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
Thanks for the reply.
But I am still not very clear what should I do.
If I keep using uri="http://java.sun.com/jsp/jstl/core",then how to write web.xml file.

If I change "uri=/WEB-INF/c.tld" can anybody write the code to give me the detail how to declaring a uri attribute in the directive.

I just begin to study HFSJ. Please help me. Thank you.

web.xml file I wrote, please add something I missed:
-------------------
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<servlet>
<servlet-name>name</servlet-name>
<servlet-class>JstlTest</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>/hello.do</url-pattern>
</servlet-mapping>

</web-app>
----------------------------------------------------------

Anna
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Anna Wang:
If I change "uri=/WEB-INF/c.tld" can anybody write the code to give me the detail how to declaring a uri attribute in the directive.


What code? Just change the uri attribute. Take out the current value you set for the uri and replace it with the actual path to the tld file.
Selvaraj Subramanian
Ranch Hand

Joined: Jul 15, 2005
Posts: 37
Anna, If u use JSP 2.0, u no need to mention <taglib-location> using <taglib> in web.xml.
You just give the path of the tld file like "WEB-INF/c.tld"(if it exists) otherwise u can extract the c.tld from "standard.jar" or check the URI in c.tld of "standard.jar"

and also u have put the attribute name as "item", it should be items

<c:forEach var="movie" items="${movieList}">
[ June 20, 2006: Message edited by: Selvaraj Subramanian ]

Selvaraj.S<br />SCJP 1.4 -83%<br />SCWCD 1.4 - 85%<br />SCEA 4 Part 1(310-051) - 89%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Anna,
did you put standard.jar in your lib directory as well ?

And have a look at this FAQ :
http://faq.javaranch.com/view?JstlTagLibDefinitions


[My Blog]
All roads lead to JavaRanch
Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
Thank you so very much for the answer. It works now.

I made two mistake. First I did not put standard.jar in the lib, second item should be items.

Thanks again.

Anna
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help-standard tag c:forEach practice