Article Title: use jsp custom tag library to display the simulated cms4j tag library effect in the data list
Author: javaboy2012
Email: yanek@163.com
Qq: 1046011462
Call method in cms4j:
The following example achieves similar results:
Running effect:
The details are as follows:
Jsp call code:
<% @ Page language = "java" import = "java. util. *, com. yanek. cms. vo. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/tags/my-cms" prefix = "myTag" %>
<Body>
<MyTag: articleListTag cateid = "1">
<% = Article_info.getId () %> ------
<% = Article_info.getTitle () %> <br>
$ {Article_info.id} ---- $ {article_info.title} <br>
</MyTag: articleListTag>
<Hr>
<MyTag: articleListTag cateid = "2">
<% = Article_info.getId () %> ------
<% = Article_info.getTitle () %> <br>
$ {Article_info.id} ---- $ {article_info.title} <br>
</MyTag: articleListTag>
</Body>
</Html>
Tag library Definition
<! -- ArticleListTag start -->
<Tag>
<Name> articleListTag </name>
<Tag-class> com. yanek. cms. tag. ArticleTag </tag-class>
<Body-content> jsp </body-content>
<Variable>
<Name-given> article_info </name-given>
<! -- <Name-from-attribute> name </name-from-attribute> -->
<Variable-class> com. yanek. cms. tag. Article </variable-class>
<Declare> true </declare>
<Scope> NESTED </scope>
</Variable>
<Attribute>
<Name> cateid </name>
<Required> true </required>
</Attribute>
</Tag>
<! -- ArticleListTag end -->
Tag Library Class
Package com. yanek. cms. tag;
Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. tagext. BodyTagSupport;
Public class ArticleTag extends BodyTagSupport {
Private Iterator it; // the object to be iterated
Private int cateid; // document category id
Public final static String name = "article_info ";
@ Override
Public int doEndTag () throws JspException {
Try {
If (bodyContent! = Null ){
BodyContent. writeOut (bodyContent. getEnclosingWriter ());
}
} Catch (IOException e ){
E. printStackTrace ();
}
Return EVAL_PAGE;
}
@ Override
Public int doStartTag () throws JspException {
// Here, different list data is constructed based on the document category, which can be obtained based on the database.
List <Article> articles = new ArrayList <Article> ();
If (cateid = 1 ){
Articles. add (new Article (1, "asp "));
Articles. add (new Article (2, "jsp "));
} Else {
Articles. add (new Article (3, "php "));
Articles. add (new Article (4, "java "));
}
It = articles. iterator ();
If (it = null ){
Return SKIP_BODY;
} Else {
Return continueNext ();
}
}
Private int continueNext (){
If (it. hasNext ()){
PageContext. setAttribute (name, it. next (), pageContext. PAGE_SCOPE );
Return EVAL_BODY_TAG;
} Else {
Return SKIP_BODY;
}
}
@ Override
Public int doAfterBody (){
Return continueNext ();
}
Public int getCateid (){
Return cateid;
}
Public void setCateid (int cateid ){
This. cateid = cateid;
}
}
Tag library reference object class
Package com. yanek. cms. tag;
Public class Article {
Private int id;
Private String title;
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String gettitle (){
Return title;
}
Public void settitle (String title ){
This. Title = title;
}
Public article (int id, String title ){
Super ();
This. ID = ID;
This. Title = title;
}
Public article (){
Super ();
This. ID = ID;
This. Title = title;
}
}
Note: The name of the script variable article_info used to display objects in the list is defined in the tag library definition file and tag Library Class, as follows:
<Variable>
<Name-given> article_info </name-given>
<! -- <Name-from-attribute> name </name-from-attribute> -->
<Variable-class> com. yanek. cms. tag. Article </variable-class>
<Declare> true </declare>
<Scope> NESTED </scope>
</Variable>
Tag Library Class
Public final static String name = "article_info ";
PageContext. setAttribute (name, it. next (), pageContext. PAGE_SCOPE );
There are two ways to display objects in the list:
Method of calling class: You can call the method prompt in eclipse.
<% = Article_info.getId () %> ---- <% = article_info.getTitle () %>
Jstl display:
$ {Article_info.id} ---- $ {article_info.title}