Servlet 自定义标签
自定义标签
1)用户定义的一种jsp标记,当一个含有自定义标签的jsp页面被jsp引擎编译成servlet时,tag标签被转化成了对一个称为 标签处理类 的对象的操作。于是,当jsp页面被jsp引擎转化为servlet后,实际上tag标签被转化为了对tag处理类的操作。
2)所有的标签处理器类都要实现 JspTag 接口,该接口中没有定义任何方法,主要作为 Tag 和 SimpleTag 接口的父接口。编写完成标签功能的 Java 类(标签处理器)
public class ForEachTag extends SimpleTagSupport{
private Collection<?> items;//集合变量名
public void setItems(Collection<?> items) {
this.items = items;
}
private String var;//变量
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
if(items != null){
for(Object obj: items){
// 遍历的对象放入到 pageContext
getJspContext().setAttribute(var, obj);
//把标签体的内容直接输出到页面上.
getJspBody().invoke(null);
}
}
}
}
3)编写标签库描述(tld)文件,在tld文件中对自定义中进行描述
标签库描述(Tag Library Description)文件简称为 tld 文件,其扩展名为 .tld,多个标签的集合就形成了一个标签库,标签库中的所有标签都必须在标签文件中进行描述。Tld 文件可以放置在 web 应用程序的 WEB-INF 目录及其子目录中,但不能放置在 WEB-INF 目录下的 classes 和 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>MyTag 1.2 core library</description>
<display-name>MyTag core</display-name>
<tlib-version>1.2</tlib-version>
<short-name>myself</short-name>
<uri>http://myself.com/myTag/core</uri>
<tag>
<name>forEach</name>
<tag-class>com.demo.tag.ForEachTag</tag-class>
<body-content>scriptless</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>
<tag>
<name>choose</name>
<tag-class>com.demo.tag.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>com.demo.tag.WhenTag</tag-class>
<body-content>scriptless</body-content> <attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>com.demo.tag.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag> </taglib>
4) 在 JSP 页面使用 taglib 指令引入标签库描述文件:
<%@ taglib prefix=“” uri=“” %> uri:属性用于指定所引入的标签库描述(tld)文件中所定义的 <uri> 元素的内容;prefix 属性用于为引入的 tld 文件指定一个”引用代号”。Prefix 属性可以由 jsp 文件的作者任意指定,只要与其他 taglib 指令的 prefix 属性值不同就可以。
<%@page import="com.demo.tag.Customer"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="myself" uri="http://myself.com/myTag/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>show mytag</title>
</head>
<body>
<%
List<Customer> customers = new ArrayList<Customer>();
customers.add(new Customer(1, "AAA"));
customers.add(new Customer(2, "BBB"));
customers.add(new Customer(3, "CCC"));
customers.add(new Customer(4, "DDD"));
customers.add(new Customer(5, "EEE"));
request.setAttribute("customers", customers);
%>
<myself:forEach items="${requestScope.customers }" var="cust">
--${pageScope.cust.id } -- ${cust.name } <br>
</myself:forEach>
<br><br>
<myself:choose>
<myself:when test="${param.age > 30}">中年...</myself:when>
<myself:when test="${param.age > 20}">青年...</myself:when>
<myself:otherwise>少年...</myself:otherwise>
</myself:choose>
</body>
</html>
5)访问http://127.0.0.1:8080/demo/tag/test.jsp?age=30

Servlet 自定义标签的更多相关文章
- 小峰servlet/jsp(5)jsp自定义标签
一.自定义标签helloworld: 二.自定义有属性的标签: HelloWorldTag.java:继承TagSupport: package com.java1234.tag; import ja ...
- Servlet和JSP之自定义标签学习
此文章会讲述简单标签处理器,因为经典自定义标签处理器没有简单标签处理器方便使用,故在此不进行描述. 参考:慕课网的<JSP自定义标签>视频; <Servlet.JSP和Sprin ...
- [JSP]自定义标签库taglib
自定义标签的步骤 自定义标签的步骤大概有三步: 1.继承javax.servlet.jsp.tagext.*下提供的几个标签类,如Tag.TagSupport.BodyTagSupport.Simpl ...
- [Java] JSP笔记 - 自定义标签
自定义标签的创建步骤: 自定义标签的四大功能: 自定义标签的类结构: 在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现j ...
- 12 自定义标签/JSTL标签库/web国际化/java web之设计模式和案例
EL应用 自定义一个标签,实现两个字符串的相加 1回顾 1.1servlet生命周期 init(ServletConfig) service ...
- EL函数以及自定义标签的应用
一.EL函数(调用普通类的静态方法) 编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤): ①编写一个普通的java类,提供一个静态方法,功能自定,例如下: package cn.wzbril ...
- JSTL 自定义标签
编写描述标签的tld文件,把这个文件放到web-inf/目录下,才能在jsp页面上调用自定义的标签 package test.yz; import java.io.IOException; impor ...
- JSP自定义标签/自定义标签打包
有这样一个业务需求: 当我们在编辑某个用户时,需要设置该用户的角色,在转到编辑页面时,就需要自动勾选上该用户已经选择的角色,如下图: 当我们点击编辑时,会查询用户详细信息,以及角色集合传到编辑页面. ...
- JSP自定义标签
在JSP网页编程中,我们通常不满足于jstl或其他的框架,我们也可以自己写属于自己专用的标签. 在这里介绍一下表格中展示JavaBean的自定义标签的使用 第一步:写一个父标签,用来显示获取数据 新建 ...
随机推荐
- dijistra
#include<bits/stdc++.h> using namespace std; ,maxm = ; int begin[maxn],to[maxm],next[maxm],v[m ...
- 运行maven命令的时候出现jre不正确
出错详情: Unable to locate the Javac Compiler in: C:\Program Files\Java\jre1.6.0_07\..\lib\tools.jar Ple ...
- 注册中心(Eureka)
1. pom.xml依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</gr ...
- 网络流24题——魔术球问题 luogu 2765
题目描述:这里 这道题是网络流问题中第一个难点,也是一个很重要的问题 如果直接建图感觉无从下手,因为如果不知道放几个球我就无法得知该如何建图(这是很显然的,比如我知道 $1+48=49=7^2$ ,可 ...
- Long Long Ago 二分查找
L: Long Long Ago 时间限制: 1 s 内存限制: 128 MB 提交 我的状态 题目描述 今天SHIELD捕获到一段从敌方基地发出的信息里面包含一串被经过某种算法加密过的的序 ...
- centos 配置.Net core 环境并部署dotnet Core文件
一.配置环境[Microsoft dotnet Core] 1) Add the dotnet product feed sudo rpm -Uvh https://packages.microsof ...
- Springboot @Transactional Mysql事务 无效
JPA默认创建的表是MyISAM引擎,MyISAM引擎不支持事务操作 所以需要将将数据库引擎改为InnoDB 配置修改 spring.jpa.database-platform=org.hiberna ...
- Asp.Net Core 使用Swashbuckle.AspNetCore 生成API文档
详情参考:https://www.cnblogs.com/morang/p/9741511.html github地址:https://github.com/yimogit/moxy.blogs/tr ...
- jar文件内lib引用的jar插件修改后更新
打包的java服务在第三方jar进行修改后,要更新线上的jar包时,直接替换原有lib引用的jar文件,会造成服务起不来, 可在本地clean install之后,用线上的classes文件夹替换本地 ...
- 2013年省赛I题 Thrall’s Dream
2013年省赛I题判断单向联通,用bfs剪枝:从小到大跑,如果遇到之前跑过的点(也就是编号小于当前点的点),就o(n)传递关系. bfs #include<iostream> #inclu ...