使用标签控制页面逻辑案例:
开发<c:if>标签
1.java类:IfDemoTag.java
package com.hbsi.copyTag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import sun.java2d.pipe.SpanShapeRenderer.Simple; public class IfDemoTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { if(test){ this.getJspBody().invoke(null); } } }
2.jsp文件:ifDemo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>模仿sun公司的c:if标签</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> <c:if test="${user==null}"> This is my JSP page. <br> </c:if> </body> </html>
3.lib文件:c.lib
<?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"> <description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>c</short-name> <uri>/copy</uri> <tag> <name>if</name> <tag-class>com.hbsi.copyTag.IfDemoTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
第二种情况:
jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>模仿sun公司的c:if标签</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> <c:if test="${user!=null}"> This is my JSP page. <br> </c:if> </body> </html>
开发<c:if><c:else>标签
1.java类:
ChooseTag.java
package com.hbsi.copyTag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ChooseTag extends SimpleTagSupport { private boolean isOK; public void setOK(boolean isOK) { this.isOK = isOK; } public boolean isOK() { return isOK; } @Override public void doTag() throws JspException, IOException { this.getJspBody().invoke(null); } }
WhenTag.java
package com.hbsi.copyTag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class WhenTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { ChooseTag parent = (ChooseTag) this.getParent(); if(test && !parent.isOK()){ this.getJspBody().invoke(null); parent.setOK(true); } } }
OtherWiseTag.java
package com.hbsi.copyTag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class OtherWiseTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { ChooseTag parent = (ChooseTag) this.getParent(); if(!parent.isOK()){ this.getJspBody().invoke(null); parent.setOK(true); } } }
2.jsp文件:
choose.java
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'choose.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> <c:choose> <c:when test="<%=true %>">aaaa</c:when> <c:othewise>vvvv</c:othewise> </c:choose> </body> </html>
3.lib文件:
c.lib
<?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"> <description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>c</short-name> <uri>/copy</uri> <tag> <name>if</name> <tag-class>com.hbsi.copyTag.IfDemoTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>choose</name> <tag-class>com.hbsi.copyTag.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>com.hbsi.copyTag.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>othewise</name> <tag-class>com.hbsi.copyTag.OtherWiseTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'choose.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> <c:choose> <c:when test="<%=false %>">aaaa</c:when> <c:othewise>vvvv</c:othewise> </c:choose> </body> </html>
开发列表遍历标签
1.java类:
ForEachTag.java
package com.hbsi.copyTag; 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 ForEachTag extends SimpleTagSupport { private String var; private List items; public void setVar(String var) { this.var = var; } public void setItems(List items) { this.items = items; } @Override public void doTag() throws JspException, IOException { Iterator it = items.iterator(); while(it.hasNext()){ Object obj = it.next(); //放到域里去,取出来的一个对象放到var属性里 this.getJspContext().setAttribute(var, obj); //执行标签体的内容 this.getJspBody().invoke(null); } } }
2.jsp文件:
forEach.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>forEach</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> <% List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); request.setAttribute("list",list); %> <c:forEach items="${list}" var="str">${str}</c:forEach> </body> </html>
3.lib文件:
c.jsp
<?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"> <description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>c</short-name> <uri>/copy</uri> <tag> <name>if</name> <tag-class>com.hbsi.copyTag.IfDemoTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>choose</name> <tag-class>com.hbsi.copyTag.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>com.hbsi.copyTag.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>othewise</name> <tag-class>com.hbsi.copyTag.OtherWiseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>forEach</name> <tag-class>com.hbsi.copyTag.ForEachTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
开发迭代标签
1.java类:
ForEachAllTag.java
package com.hbsi.copyTag; import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ForEachAllTag extends SimpleTagSupport { // 标签处理类里的变量,不管传什么都转成Collectiion类型 private Collection collection; // var和items是标签属性里要用到的属性变量 private String var; // 迭代所有集合(map,list,a[]),用object private Object items; public void setVar(String var) { this.var = var; } // 执行到标签,给标签赋值的时候,jsp引擎会调用set方法 public void setItems(Object items) { this.items = items; if(items instanceof Map){ //items对象转成map Map map = (Map) items; //多态,返回set对象 collection = map.entrySet(); } if(items instanceof Collection){ //set list collection = (Collection) items; } //这么判断不行,数组只能取到类类型,取不到一般数据类型 // if (items instanceof Object) {} if(items.getClass().isArray()){ //取到集合赋给list collection = new ArrayList(); int len = Array.getLength(items); for(int i=0;i<len;i++){ Object obj = Array.get(items,i); collection.add(obj); } } } @Override public void doTag() throws JspException, IOException { Iterator it = collection.iterator(); while(it.hasNext()){ Object obj = it.next(); this.getJspContext().setAttribute(var, obj); this.getJspBody().invoke(null); } } }
2.jsp文件:
forEachAll.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>遍历所有集合标签</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"> </head> <body> <% List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); request.setAttribute("list",list); %> <c:forEachAll var="str" items="${list}">${str}</c:forEachAll> <br/> <br/> <% Map map = new HashMap(); map.put("aa","aaa"); map.put("bb","bbb"); map.put("cc","ccc"); map.put("dd","ddd"); request.setAttribute("map",map); %> <c:forEachAll items="${map}" var="str">${str.key}-----${str.value}<br></c:forEachAll> <br/> <br/> <% String strs[] = {"aaa","bbb","ccc"}; request.setAttribute("strs",strs); %> <c:forEachAll items="${strs}" var="str">${str}<br></c:forEachAll> <br/> <br/> <% int i[] = {1,2,3}; request.setAttribute("i",i); %> <c:forEachAll items="${i}" var="num">${num}<br></c:forEachAll> <br/> <br/> </body> </html>
3.lib文件:
c.lib
<?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"> <description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>c</short-name> <uri>/copy</uri> <tag> <name>if</name> <tag-class>com.hbsi.copyTag.IfDemoTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>choose</name> <tag-class>com.hbsi.copyTag.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>com.hbsi.copyTag.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>othewise</name> <tag-class>com.hbsi.copyTag.OtherWiseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>forEach</name> <tag-class>com.hbsi.copyTag.ForEachTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>forEachAll</name> <tag-class>com.hbsi.copyTag.ForEachAllTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
开发html转义标签
1.java类:
HTMLFilter.java
package com.hbsi.copyTag; import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; import com.sun.net.httpserver.Filter; public class HTMLFilterTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf = this.getJspBody(); StringWriter sw = new StringWriter(); jf.invoke(sw); String s = sw.toString(); s = filter(s); this.getJspContext().getOut().write(s); } public static String filter(String message) { if (message == null) return (null); char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return (result.toString()); } }
2.jsp文件:
htmlFilter.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>文本输出</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"> </head> <body> <c:htmlfilter> <a href="">aaa</a> </c:htmlfilter> </body> </html>
3.lib文件:
c.lib
<?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"> <description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>c</short-name> <uri>/copy</uri> <tag> <name>if</name> <tag-class>com.hbsi.copyTag.IfDemoTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>choose</name> <tag-class>com.hbsi.copyTag.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>com.hbsi.copyTag.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>othewise</name> <tag-class>com.hbsi.copyTag.OtherWiseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>forEach</name> <tag-class>com.hbsi.copyTag.ForEachTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>forEachAll</name> <tag-class>com.hbsi.copyTag.ForEachAllTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>htmlfilter</name> <tag-class>com.hbsi.copyTag.HTMLFilterTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
不使用标签:
jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/copy" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>文本输出</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"> </head> <body> <a href="">aaa</a> </body> </html>
打包标签库
1.新建java项目
2.把要打包的文件考到java项目里
3.考tld文件
项目下新建文件夹MATE-INF
Tld文件拷到此文件夹下
4.新建lib文件夹,导入web所需的包
5.添加path路径
6.java项目右键导出
新建一个WEB项目测试
复制jar包考到lib下
编写一个jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://www.hbsi.com" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>测试JAR包</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> <c:htmlfilter> <a href="">This is my JSP page.</a> <br> </c:htmlfilter> <br> </body> </html>