I want to define a very simple JSP 2.0 tag, but don't know how to do.
The tag is used to get the information of a article
object in the system. And it's very simple:
<sys:article id="123" var="article">
Title: ${aritcle.title}
</sys:article>
Then I created a file named article.tag
under WEB-INF/tags/
, the content is:
<% tag import="sys.App, sys.domains.*" %>
<%@ attribute
name="id"
type="java.lang.String"
required="true"
description="the id of an article" %>
<%@ attribute
name="var"
type="java.lang.String"
required="true"
description="let invoker use it to get information" %>
<%
Article article = App.articleDao.get(id);
%>
// how to set article to the body
<jsp:doBody />
I don't know how to set the retrieved article object to the body, and let it show the title of it.
The key is variable
in the tag file.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute
name="id"
required="true"
description="the id of article" %>
<%@ attribute
name="var"
required="true"
rtexprvalue="false"
description="the article object used by invoker" %>
<%@ variable
name-from-attribute="var"
description="The alias of passed 'var'"
alias="aaa"
%>
<%
Article article = getArticleById(id);
request.setAttribute("article", article);
%>
<c:set var="aaa" value="${article}" />