src/main/java |---- cn.ljl.javaweb.demo.jsp2taglib.servlet |---- HelloWorldServlet.java |---- cn.ljl.javaweb.demo.jsp2taglib |---- HelloWordTag.java src/main/webapp |---- WEB-INF |---- jsp2taglib |---- demo.tld |---- demo/jsp2taglib |---- helloword.jsp
package cn.ljl.javaweb.demo.jsp2taglib; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * 一个简单的标签实现类,在页面输出一行hello word内容. * @author lijinlong * */ public class HelloWordTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { getJspContext().getOut().print("Hello World" + new java.util.Date()); } }
<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>demo</short-name> <uri>http://www.lijinlong.cn/demotaglib</uri> <tag> <description>输出Hello World,这个标签没有属性和标签体</description> <name>helloword</name> <tag-class>cn.ljl.javaweb.demo.jsp2taglib.HelloWordTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://www.lijinlong.cn/demotaglib" prefix="demo"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>自定义标签 - Hello World</title> </head> <body> <h2>显示自定义标签 - Hello World的内容:</h2> <demo:helloword /> </body> </html>
package cn.ljl.javaweb.demo.jsp2taglib.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name="helloWorldServlet", urlPatterns = {"/demo/jsp2taglib/helloword"}) public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = -297481388442806334L; private static final String VIEW_HELLOWORLD = "/demo/jsp2taglib/helloword.jsp"; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher(VIEW_HELLOWORLD).forward(req, resp); } }
src/main/java |---- cn.ljl.javaweb.demo.jsp2taglib.servlet |---- AddServlet.java |---- cn.ljl.javaweb.demo.jsp2taglib |---- AddTag.java src/main/webapp |---- WEB-INF |---- jsp2taglib |---- demo.tld |---- demo/jsp2taglib |---- add.jsp
package cn.ljl.javaweb.demo.jsp2taglib; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * 用来演示带属性的标签.<br/> * 实现求和的功能. * @author lijinlong * */ public class AddTag extends SimpleTagSupport { private int arg1; private int arg2; public int getArg1() { return arg1; } public void setArg1(int arg1) { this.arg1 = arg1; } public int getArg2() { return arg2; } public void setArg2(int arg2) { this.arg2 = arg2; } @Override public void doTag() throws JspException, IOException { int result = arg1 + arg2; String template = "the result of arg1 + arg2 is:%s"; String content = String.format(template, String.valueOf(result)); JspWriter out = getJspContext().getOut(); out.print(content); } }
<tag> <description>实现求和的功能 - 用来演示带属性的标签</description> <name>add</name> <tag-class>cn.ljl.javaweb.demo.jsp2taglib.AddTag</tag-class> <body-content>empty</body-content> <attribute> <name>arg1</name> <required>true</required> <fragment>true</fragment> </attribute> <attribute> <name>arg2</name> <required>true</required> <fragment>true</fragment> </attribute> </tag>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://www.lijinlong.cn/demotaglib" prefix="demo"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>自定义标签 - Add</title> </head> <body> <h2>求3+5的和:</h2> <demo:add arg1="3" arg2="5" /> </body> </html>
package cn.ljl.javaweb.demo.jsp2taglib.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "addServlet", urlPatterns = { "/demo/jsp2taglib/add" }) public class AddServlet extends HttpServlet { private static final long serialVersionUID = 3526679109725991549L; private static final String VIEW_ADD = "/demo/jsp2taglib/add.jsp"; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher(VIEW_ADD).forward(req, resp); } }
src/main/java |---- cn.ljl.javaweb.demo.jsp2taglib.servlet |---- IteratorServlet.java |---- cn.ljl.javaweb.demo.jsp2taglib |---- IteratorTag.java src/main/webapp |---- WEB-INF |---- jsp2taglib |---- demo.tld |---- demo/jsp2taglib |---- iterator.jsp
package cn.ljl.javaweb.demo.jsp2taglib; import java.io.IOException; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * 实现迭代输出器的功能.<br> * 用来演示带有标签体的标签. * * @author lijinlong * */ public class IteratorTag extends SimpleTagSupport { private List<String> collection; private String eleAttrName; public List<String> getCollection() { return collection; } public void setCollection(List<String> collection) { this.collection = collection; } public String getEleAttrName() { return eleAttrName; } public void setEleAttrName(String eleAttrName) { this.eleAttrName = eleAttrName; } @Override public void doTag() throws JspException, IOException { for (String ele : collection) { getJspContext().setAttribute(eleAttrName, ele); getJspBody().invoke(null); } } }
<tag> <description>迭代输出所有字符串</description> <name>iterator</name> <tag-class>cn.ljl.javaweb.demo.jsp2taglib.IteratorTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>collection</name> <required>true</required> <fragment>true</fragment> </attribute> <attribute> <name>eleAttrName</name> <required>true</required> <fragment>true</fragment> </attribute> </tag>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://www.lijinlong.cn/demotaglib" prefix="demo"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>自定义标签 - iterator</title> </head> <body> <h2>我们给您的建议:</h2> <ol> <demo:iterator collection="${requestScope.adviceList}" eleAttrName="item"> <li>${pageScope.item}</li> </demo:iterator> </ol> </body> </html>
package cn.ljl.javaweb.demo.jsp2taglib.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "iteratorServlet", urlPatterns = { "/demo/jsp2taglib/iterator" }) public class IteratorServlet extends HttpServlet { private static final long serialVersionUID = 4201830343915991973L; private static final String VIEW_SHOW = "/demo/jsp2taglib/iterator.jsp"; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<String> advices = new ArrayList<String>(); advices.add("早睡早起。"); advices.add("坚持锻炼身体。"); advices.add("每天留半个小时发呆或者冥想。"); advices.add("保持心情平和。"); advices.add("专注认真但不要急功近利。"); advices.add("赶紧脱单。"); req.setAttribute("adviceList", advices); req.getRequestDispatcher(VIEW_SHOW).forward(req, resp); } }
src/main/java |---- cn.ljl.javaweb.demo.jsp2taglib.domain |---- Topic.java |---- cn.ljl.javaweb.demo.jsp2taglib.servlet |---- TopicServlet.java |---- cn.ljl.javaweb.demo.jsp2taglib |---- TopicTag.java src/main/webapp |---- WEB-INF |---- jsp2taglib |---- demo.tld |---- demo/jsp2taglib |---- topic.jsp
package cn.ljl.javaweb.demo.jsp2taglib.domain; /** * 这是一个pojo,用于封装一个问题及其唯一的答案. * @author lijinlong * */ public class Topic { private String question; private String answer; public Topic(String question, String answer) { super(); this.question = question; this.answer = answer; } public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } }
package cn.ljl.javaweb.demo.jsp2taglib; import java.io.IOException; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; import cn.ljl.javaweb.demo.jsp2taglib.domain.Topic; /** * 用来演示支持JspFragment的标签.<br> * 这个标签将迭代一个话题的列表,允许jsp分别定义如何输出问题和答案. * @author lijinlong * */ public class TopicTag extends SimpleTagSupport { /** 话题列表 */ private List<Topic> topicList; /** 问题attribute的name */ private String quesAttrName; /** 答案attribute的name */ private String answAttrName; /** 问题的JspFragment */ private JspFragment quesFrag; /** 答案的JspFragment */ private JspFragment answFrag; public List<Topic> getTopicList() { return topicList; } public void setTopicList(List<Topic> topicList) { this.topicList = topicList; } public String getQuesAttrName() { return quesAttrName; } public void setQuesAttrName(String quesAttrName) { this.quesAttrName = quesAttrName; } public String getAnswAttrName() { return answAttrName; } public void setAnswAttrName(String answAttrName) { this.answAttrName = answAttrName; } public JspFragment getQuesFrag() { return quesFrag; } public void setQuesFrag(JspFragment quesFrag) { this.quesFrag = quesFrag; } public JspFragment getAnswFrag() { return answFrag; } public void setAnswFrag(JspFragment answFrag) { this.answFrag = answFrag; } @Override public void doTag() throws JspException, IOException { for (Topic t : topicList) { getJspContext().setAttribute(quesAttrName, t.getQuestion()); getJspContext().setAttribute(answAttrName, t.getAnswer()); quesFrag.invoke(null); answFrag.invoke(null); } } }
<tag> <description>输出话题</description> <name>topic</name> <tag-class>cn.ljl.javaweb.demo.jsp2taglib.TopicTag</tag-class> <body-content>empty</body-content> <attribute> <name>topicList</name> <required>true</required> <fragment>true</fragment> </attribute> <attribute> <name>quesAttrName</name> <required>true</required> <fragment>true</fragment> </attribute> <attribute> <name>answAttrName</name> <required>true</required> <fragment>true</fragment> </attribute> <attribute> <name>quesFrag</name> <required>true</required> <fragment>true</fragment> </attribute> <attribute> <name>answFrag</name> <required>true</required> <fragment>true</fragment> </attribute> </tag>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://www.lijinlong.cn/demotaglib" prefix="demo"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>自定义标签 - topic</title> </head> <body> <h2>我们讨论了下述的问题,并统一了其答案:</h2> <demo:topic topicList="${requestScope.topicList }" quesAttrName="ques" answAttrName="answ"> <jsp:attribute name="quesFrag"> <div style="font-weight: bold;"> ${pageScope.ques} </div> </jsp:attribute> <jsp:attribute name="answFrag"> <div style="margin-top: 5px; margin-bottom: 15px;"> ${pageScope.answ} </div> </jsp:attribute> </demo:topic> </body> </html>
package cn.ljl.javaweb.demo.jsp2taglib.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.ljl.javaweb.demo.jsp2taglib.domain.Topic; @WebServlet(name = "topicServlet", urlPatterns = { "/demo/jsp2taglib/topic" }) public class TopicServlet extends HttpServlet { private static final long serialVersionUID = -2717086554249858215L; private static final String VIEW_SHOW = "/demo/jsp2taglib/topic.jsp"; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Topic> topicList = new ArrayList<Topic>(); topicList.add(new Topic("我很想做一件事,但是又害怕失败,怎么办?", "害怕失败比失败本身更可怕。失败只是一时的,害怕失败却会纠缠你很久。")); topicList.add(new Topic("最近被一些人否定了,我觉得自己很没用。", "别人轻视了你,是轻视你的过往;你轻视了自己,是放弃了你的未来。平静地对待,平静地加油。")); topicList.add(new Topic("我被一个同学骗了,再也不敢相信任何人了。", "所有的人都靠谱,就没有欺骗;所有的人都不靠谱,就欺骗不了。就是有人不可信,才会有些人可信,你要学会鉴别,不要失望。")); req.setAttribute("topicList", topicList); req.getRequestDispatcher(VIEW_SHOW).forward(req, resp); } }
src/main/java |---- cn.ljl.javaweb.demo.jsp2taglib.servlet |---- DynaAttrServlet.java |---- cn.ljl.javaweb.demo.jsp2taglib |---- DynamicAttributesTag.java src/main/webapp |---- WEB-INF |---- jsp2taglib |---- demo.tld |---- demo/jsp2taglib |---- dynaAttr.jsp
package cn.ljl.javaweb.demo.jsp2taglib; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.DynamicAttributes; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * 动态属性标签库.<br> * 不限制属性的个数和属性的名称.<br> * @author lijinlong * */ public class DynamicAttributesTag extends SimpleTagSupport implements DynamicAttributes { private List<String> keys = new ArrayList<String>(); private List<Object> values = new ArrayList<Object>(); @Override public void setDynamicAttribute(String uri, String localName, Object value) throws JspException { keys.add(localName); values.add(value); } @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("<ol>"); for (int i = 0; i < keys.size(); i ++) { String key = keys.get(i); Object value = values.get(i); out.println("<li>" + key + "=" + value + "</li>"); } out.println("</ol>"); } }
<tag> <name>dynaAttr</name> <tag-class>cn.ljl.javaweb.demo.jsp2taglib.DynamicAttributesTag</tag-class> <body-content>empty</body-content> <dynamic-attributes>true</dynamic-attributes> </tag>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://www.lijinlong.cn/demotaglib" prefix="demo"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>自定义标签 - dynaAttr</title> </head> <body> <h2>指定了三个属性:</h2> <demo:dynaAttr name="Adam" age="24" favorite="books" /> </body> </html>
package cn.ljl.javaweb.demo.jsp2taglib.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name="dynaAttrServlet", urlPatterns={"/demo/jsp2taglib/dynaAttr"}) public class DynaAttrServlet extends HttpServlet { private static final long serialVersionUID = -3146911293779890912L; private static final String VIEW_SHOW = "/demo/jsp2taglib/dynaAttr.jsp"; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher(VIEW_SHOW).forward(req, resp); } }