本文共 5094 字,大约阅读时间需要 16 分钟。
07820170914jstl自定义标签:主要用于移除Jsp页面中的java代码1、编写一个类,继承SimpleTagSupport[类属性与配置在tld中的属性参数一致,添加set、get方法]2、重写doTage(标签的核心方法,输出标签的内容)3、编写一个tld文件(描述标签的属性[标签名、对应的类、对应的uri...])package com.tiger.tag;import java.io.IOException;import java.util.Map;import java.util.Set;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * 遍历集合,nobody * @author tiger * @time 2017年9月13日 */public class MySelect extends SimpleTagSupport{ //设置参数,与.tld文件中attribute中的name一致 private Map map; private String path; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } @Override public void doTag() throws JspException, IOException { //内置一个pageContext对象,它里面是封装了9个隐式对象 StringBuffer htmlBuffer=new StringBuffer(); //遍历集合元素 htmlBuffer.append(" "); Set set = map.keySet(); for (Object key : set) { htmlBuffer.append(""); htmlBuffer.append(map.get(key)); htmlBuffer.append(" "); } htmlBuffer.append(" "); getJspContext().getOut().println(htmlBuffer.toString()); }}package com.tiger.tag;import java.io.IOException;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * 遍历集合,hasbody * @author tiger * @time 2017年9月13日 */public class MySelectBody extends SimpleTagSupport{ //设置参数,与.tld文件中attribute中的name一致 private Map map; private String path; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println(" "); Iterator it = map.keySet().iterator(); while (it.hasNext()) { getJspContext().setAttribute("map", map.get(it.next())); getJspBody().invoke(null); } out.println(""); } } JSTL 1.1 core library JSTL core 1.1 tiger http://www.tiger.com/tigertag table com.tiger.tag.MyTable empty collection true true select com.tiger.tag.MySelect empty map true true path true true selectbody com.tiger.tag.MySelectBody scriptless map true true path true true <%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <%@page import="java.util.Collection"%> <%@page import="java.util.Collections"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="my" uri="http://www.tiger.com/tigertag"%> <% Map map = new HashMap (); map.put("001", "tiger"); map.put("002", "hello"); map.put("003", "xixi"); pageContext.setAttribute("map", map); %> <%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <%@page import="java.util.Collection"%> <%@page import="java.util.Collections"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="my" uri="http://www.tiger.com/tigertag"%> ----自定义标签库-hasbody---- <% Map map = new HashMap (); map.put("001", "body"); map.put("002", "呵呵"); map.put("003", "嘻嘻"); pageContext.setAttribute("map", map); %> ${map}
转载地址:http://vjeof.baihongyu.com/