博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义jstl标签用法案例
阅读量:2048 次
发布时间:2019-04-28

本文共 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/

你可能感兴趣的文章
(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)
查看>>
(1129) Recommendation System 排序
查看>>
PAT1090 Highest Price in Supply Chain 树DFS
查看>>
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【UML】《Theach yourself uml in 24hours》——hour2&hour3
查看>>
【linux】nohup和&的作用
查看>>
【UML】《Theach yourself uml in 24hours》——hour4
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【深度学习】GRU的结构图及公式
查看>>
【python】re模块常用方法
查看>>
【JavaScript】call()和apply()方法
查看>>
【JavaScript】箭头函数与普通函数的区别
查看>>