Hi Everyone..
In the previouse post we have seen simple example.Now we will see little complex example.
Run movieTag.jsp in the uploaded code to execute the custom tag.
I want to develop a custom tag.. to display the set of movies stored in page scope.
My tag should be like this and jsp page should display movies when the following
custo tag is encountered.
<mytags:iterateMovies>
${movie}<br>
</mytags:iterateMovies>
movieTag.jsp
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="mytags" uri="/WEB-INF/tlds/hello" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
List movies = new ArrayList();
movies.add("Three Idiots");
movies.add("Titanic");
pageContext.setAttribute("movies", movies);
%>
<mytags:iterateMovies>
${movie}<br>
</mytags:iterateMovies>
</body>
</html>
Whatever the content in between Opening tag (<mytags:iterateMovies>) and Ending tag( </mytags:iterateMovies>) is called body or body content(JspFragment)
If you want to display the body content on the jsp page from doTag,
you have to say getJspBody().invoke(null);
getJspBody() method is available in SimpleTagSupport class.
But the body content is ${movie} in the above example, so you have to set movie in page scope.
If you observe the doTag() code...
1)First I have retrieved.. the movies list from page scope.
2)In second stage, I am iterating each movie and setting one movie
at a time in to page scope with name movie.
3)Whenever.. I say getJspBody().invoke(null), the body content between
opening tag and closing tag (${movie}) should be displayed to jsp but
${movie} is expression language, so the movie set in page scope will
replace the ${movie} and movie is displayed on jsp page. For every iteration the
${movie} will be replaced with movie from list and all the movies get displayed.
public void doTag() throws JspException, IOException {
List movies = (List)getJspContext().getAttribute("movies");
for(int i=0;i<movies.size();i++){
getJspContext().setAttribute("movie", movies.get(i));
getJspBody().invoke(null);
}
}
How getJspBody().invoke(null) works ?
Suppose if your custom tag is
<mytags:testBody>Hello I am BODY </mytags:testBody>
doTag:
----------
If you want to display Hello I am BODY from.. doTag
you call getJspBody().invoke(null)
If you call getJspBody().invoke(null) 4 times from doTag
HelloI am BODY will be printed 4 times on jsp page...
Uploaded the HelloWorldWithAdvancedCustomTag in the following URL.
https://docs.google.com/file/d/0BwZaaDwCofcNMVc2dUk5TGJ1bVk/edit?usp=sharing
Let me know if you have any doubts....
In the previouse post we have seen simple example.Now we will see little complex example.
Run movieTag.jsp in the uploaded code to execute the custom tag.
I want to develop a custom tag.. to display the set of movies stored in page scope.
My tag should be like this and jsp page should display movies when the following
custo tag is encountered.
<mytags:iterateMovies>
${movie}<br>
</mytags:iterateMovies>
movieTag.jsp
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="mytags" uri="/WEB-INF/tlds/hello" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
List movies = new ArrayList();
movies.add("Three Idiots");
movies.add("Titanic");
pageContext.setAttribute("movies", movies);
%>
<mytags:iterateMovies>
${movie}<br>
</mytags:iterateMovies>
</body>
</html>
Whatever the content in between Opening tag (<mytags:iterateMovies>) and Ending tag( </mytags:iterateMovies>) is called body or body content(JspFragment)
If you want to display the body content on the jsp page from doTag,
you have to say getJspBody().invoke(null);
getJspBody() method is available in SimpleTagSupport class.
But the body content is ${movie} in the above example, so you have to set movie in page scope.
If you observe the doTag() code...
1)First I have retrieved.. the movies list from page scope.
2)In second stage, I am iterating each movie and setting one movie
at a time in to page scope with name movie.
3)Whenever.. I say getJspBody().invoke(null), the body content between
opening tag and closing tag (${movie}) should be displayed to jsp but
${movie} is expression language, so the movie set in page scope will
replace the ${movie} and movie is displayed on jsp page. For every iteration the
${movie} will be replaced with movie from list and all the movies get displayed.
public void doTag() throws JspException, IOException {
List movies = (List)getJspContext().getAttribute("movies");
for(int i=0;i<movies.size();i++){
getJspContext().setAttribute("movie", movies.get(i));
getJspBody().invoke(null);
}
}
How getJspBody().invoke(null) works ?
Suppose if your custom tag is
<mytags:testBody>Hello I am BODY </mytags:testBody>
doTag:
----------
If you want to display Hello I am BODY from.. doTag
you call getJspBody().invoke(null)
If you call getJspBody().invoke(null) 4 times from doTag
HelloI am BODY will be printed 4 times on jsp page...
Uploaded the HelloWorldWithAdvancedCustomTag in the following URL.
https://docs.google.com/file/d/0BwZaaDwCofcNMVc2dUk5TGJ1bVk/edit?usp=sharing
Let me know if you have any doubts....
No comments:
Post a Comment