JavaWeb----自定义标签的开发
来源:互联网 发布:怎么用记事本编程 编辑:程序博客网 时间:2024/06/05 17:42
最大值标签:
MaxNumberTag.java
package cn.limbo.tags;import javax.servlet.jsp.JspContext;import javax.servlet.jsp.JspException;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.JspTag;import javax.servlet.jsp.tagext.SimpleTag;import java.io.IOException;/** * Created by Limbo on 2016/9/21. */public class MaxNumberTag implements SimpleTag{ private String num1; private String num2; private PageContext pageContext; public void setNum1(String num1) { this.num1 = num1; } public void setNum2(String num2) { this.num2 = num2; } @Override public void doTag() throws JspException, IOException { Integer num1Integer = Integer.parseInt(num1); Integer num2Integer = Integer.parseInt(num2); Integer max = num1Integer > num2Integer ? num1Integer : num2Integer; this.pageContext.getOut().print("最大的数是: " + max); } @Override public void setParent(JspTag jspTag) { } @Override public JspTag getParent() { return null; } @Override public void setJspContext(JspContext jspContext) { this.pageContext = (PageContext) jspContext; } @Override public void setJspBody(JspFragment jspFragment) { }}myTag.tld
<tag> <name>max</name> <tag-class>cn.limbo.tags.MaxNumberTag</tag-class> <body-content>empty</body-content> <attribute> <name>num1</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>num2</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
转换大写标签:
ToUpperCaseTag.java
package cn.limbo.tags;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;import java.io.IOException;import java.io.StringWriter;/** * Created by Limbo on 2016/9/21. */public class ToUpperCaseTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { //获取标签体中的内容 StringWriter sw = new StringWriter(); getJspBody().invoke(sw); //将标签体中的内容以输出流写入到sw中,如果为null直接输出到屏幕 String content = sw.toString().toUpperCase();//转大写 //输出到页面 getJspContext().getOut().print(content); }}myTag.tld
<tag> <name>uppercase</name> <tag-class>cn.limbo.tags.ToUpperCaseTag</tag-class> <body-content>scriptless</body-content> </tag>
迭代标签:
ForEachTag.java
package cn.limbo.tags;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;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;/** * Created by Limbo on 2016/9/21. */public class ForEachTag extends SimpleTagSupport{ private Collection<Object> collection; private String var; private Object items; public void setItems(Object items) { this.items = items; //c标签的源码也是这样写的 利用一堆的instanceof来判断传入参数的类型 if(items instanceof Collection) { collection = (Collection<Object>) items; } if(items instanceof Map) { Map map = (Map) items; collection = map.entrySet(); } //加上这个就可以使用8种基本类型的迭代 if(items.getClass().isArray()) //这个是最重要的,反射判断 { this.collection = new ArrayList<>(); int length = Array.getLength(items); for(int i = 0; i < length; ++i) { Object value = Array.get(items,i); collection.add(value); } } } public void setVar(String var) { this.var = var; } @Override public void doTag() throws JspException, IOException { //遍历items对应的集合 Iterator it = this.collection.iterator(); while(it.hasNext()) { Object value = it.next(); //把正在遍历的对象放到pageContext中 键:var 值:正在遍历的对象 this.getJspContext().setAttribute(var,value); //把标签体的内容输出到页面上 this.getJspBody().invoke(null); } }}myTag.tld
<tag> <name>forEachTag</name> <tag-class>cn.limbo.tags.ForEachTag</tag-class> <body-content>JSP</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>应用:
<%@ page import="cn.limbo.tags.Customer" %><%@ page import="java.util.List" %><%@ page import="java.util.ArrayList" %><%@ page import="java.util.Map" %><%@ page import="java.util.HashMap" %><%-- Created by IntelliJ IDEA. User: Limbo Date: 2016/9/21 Time: 13:56 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="limbo" uri="http://www.limbo.com/jsp/jstl/core" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <title>Hello</title></head><body> <% List<Customer> customers = new ArrayList<>(); customers.add(new Customer(1,"AA")); customers.add(new Customer(2,"BB")); customers.add(new Customer(3,"CC")); customers.add(new Customer(4,"DD")); customers.add(new Customer(5,"EE")); Map<String , List<Customer> > map = new HashMap<>(); map.put("customers",customers); request.setAttribute("map",map); %> <limbo:hello></limbo:hello> <limbo:max num1="12" num2="17"></limbo:max> <br><br> <limbo:forEachTag items="${requestScope.map}" var="cust" > <limbo:forEachTag items="${cust.value}" var="content"> ID: ${content.id} ---- Name: ${content.name}<br><br> </limbo:forEachTag> </limbo:forEachTag> <limbo:uppercase>hello world</limbo:uppercase> <%--<c:forEach items="${requestScope.map}" var="cust">--%> <%--${cust.key} ---- ${cust.value}--%> <%--</c:forEach>--%></body></html>
0 0
- JavaWeb----自定义标签的开发
- JavaWeb自定义标签的开发步骤
- JavaWeb---自定义标签的步骤
- JavaWeb-12 (自定义标签库开发&JSTL标签库)
- 深入体验JavaWeb开发内幕——自定义简单标签的开发
- javaweb中对于自定义标签的学习
- JavaWeb -- Jsp 自定义标签的使用
- Javaweb----简单自定义标签的编写
- JavaWeb开发之十七:jsp自定义标签开发入门
- javaweb jsp自定义标签
- JavaWeb---用户自定义标签
- JavaWeb-自定义标签
- javaweb 自定义标签
- javaWeb之自定义标签
- JavaWeb自定义标签
- javaWeb---自定义标签
- javaweb 自定义标签
- javaweb之自定义标签——带属性的标签
- Java中finalize()用法
- Asp.Net文件上传的基本方法
- (第四周项目3)单链表的应用(2)
- 第四周--项目4-猴子选大王
- 第四周项目1-顺序表的基本运算
- JavaWeb----自定义标签的开发
- Web-listener与filter
- android 源码权限问题
- android c++标准命名空间demo
- redis在项目中的使用
- Android调用本地相册选择图片并剪切后展示和缓存
- JAVA_OO
- 第四周项目1-建立单链表
- gcdasyncsocket address already in use