Bookmark Topic Watch Topic
  • New Topic

I have a trouble in Custom tag and I can't find any mistakes, someone else can help me to find out ?  RSS feed

 
Zhang Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
customTag

simple.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib 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-
jsptaglibrary_2_0.xsd” version=”2.0">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<!--<short-name>sim</short-name>-->
<uri>simpleTags</uri>

<tag>
<description>worst use of a custom tag</description>
<name>SimpleTagTest</name>
<tag-class>edu.city.foo.SimpleTagTest</tag-class>
<body-content>scriptless</body-content>

<attribute>
<name>movieList</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>-------------simple.tld

SimpleTagTest.java
package edu.city.foo;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTagTest extends SimpleTagSupport {
private static final long serialVersionUID = 6714301064023583139L;

private List movieList;

public void setMovieList(List movieList) {
this.movieList = movieList;
}

@Override
public void doTag() throws JspException, IOException {
Iterator<Movie> iterator = movieList.iterator();
while (iterator.hasNext()) {
Movie movies = (Movie) iterator.next();
getJspContext().setAttribute("movie", movies);
getJspBody().invoke(null);
}
}
}----------------------SimpleTagTest.java

Movie.java
package edu.city.foo;

import java.util.ArrayList;
import java.util.List;

public class Movie {
private String name;
private String genre;

public Movie(String name,String genre) {
this.name = name;
this.genre = genre;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}

public static void main(String[] args) {

Movie movie1 = new Movie("7 day","skill");
Movie movie2 = new Movie("night", "skill");

List movies = new ArrayList();
movies.add(movie1);
movies.add(movie2);

SimpleTagTest simpleTagTest = new SimpleTagTest();
simpleTagTest.setMovieList(movies);
}
}--------------------------------Movie.java

useTag.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="simpleTags" prefix="myTags" %>
<%@ page import="edu.city.foo.*" %>
<%
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 'useTag.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>
<table>
<myTags:SimpleTagTest movieList="${movie}">
<tr>
<td>${movie.name}</td>
<td>${movie.genre}</td>
</tr>
</myTags:SimpleTagTest>
</table>
</body>
</html>-----------------------------------------------useTag.jsp

 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Report post to moderator
The URI in you JSP should point to your sample.tld

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Shankar Tanikella wrote:The URI in you JSP should point to your sample.tld


That is not correct. The URI in the JSP declaration simply needs to match the URI in the .tld file. The container will match it based upon the URI.

That said, a malformed URI like "simpleTags" isn't a good choice. URI's should follow the rules of URLs so a better URI would be something along the lines of: http://some-domain-that-you-own.com/simple-tags

 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks for correcting me Bear
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
It used to be that you needed to have TLDs directly referenced by path, but since JSP 2.0 the more appropriate way to find a TLD is to search by URI. That way, not only does it not matter where the .tld file is placed (as long as it is under WEB-INF somewhere), but it can also be inside a jar file, which makes packaging and using tag libraries much easier.

There also is not need for any web.xml declarations.
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank You, for the update Bear.. It is good that it is educative as well.

Hi zhang, What is the actual problem that you are facing? if any error post the fine part of error log
 
Stefan Evans
Bartender
Posts: 1834
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
After a quick glance, I spotted a couple of issues.

1 - you need to declare the type of your tag attribute, otherwise it is assumed to be string.




2 - You need to declare/construct your list before passing it to the tag.
Normally this would be done in a servlet, and you then forward to the JSP, but for purposes of example, I will resort to scriptlet code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question more than once.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!