JSP标签库
step1:
定义一个标签处理类:改类必须实现SimpleTag接口,在实际中是拓展它的子类SimpleTagSupport。复写doTag方法
public class MyTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.print("<b>hello</b>");
}
}
step2:
一旦定义了标签处理类,下一个任务便是向服务器标识这个类,并将其与特定的XML标记名称相关联。这任务是通过一个XML格式的TLD文件来完成的。该文件包含固定的信息(如XML Schema实例声明)、一个任意短的库名称、一段简的描述及一系列标签描述,可以放在WEB-INF下面的目录里
<?xml version="1.0"?>
<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>
This exposes JSON util functions from the Struts JSON plugin
</description>
<tlib-version>1.0</tlib-version>
<short-name>mytld</short-name>
<tag>
<description>out put hello</description>
<name>test</name>
<tag-class>cn.donghua.jstl.MyTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
step3:
jsp文件。引入标签<%@ taglib uri="..." prefix="..."%>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tldTest.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>
<%@ taglib uri="/WEB-INF/tlds/mytld.tld" prefix="mytest" %>
<mytest:test/>
</body>
</html>
访问输出:hello
若要为标签库添加属性如<mytest:test attr="value"/>则需要在MyTag中增加属性set方法,如加了length属性则在Mytag有setLength()
private int length;
public void setLength(String length) throws IOException{
this.length = Integer.parseInt(length);
JspWriter out = getJspContext().getOut();
out.print(length);
}
tld文件也要添加相应的属性说明:
<tag>
<description>out put hello</description>
<name>test</name>
<tag-class>cn.donghua.jstl.MyTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>length</name>
<required>false</required>
</attribute>
</tag>
jsp文件
<body>
<%@ taglib uri="/WEB-INF/tlds/mytld.tld" prefix="mytest" %>
<mytest:test length="56"/>
</body>
输出:56
最新文章
- 30分钟学会如何使用Shiro
- MSSQL数据的批量插入
- mfc ui2
- spring--事务原理
- KindEditor Asp.net
- Laravel Eloquent 的条件不等于
- 【莫队】bzoj 3781,bzoj 2038,bzoj 3289
- Struts2配置文件_常量属性_独立测试分析
- css3 翻书效果
- python之json
- line-height下的dispay:inline-block垂直居中
- JSON对象转换成字符串【JSON2.JS】
- VB 如何调用 c++ DLL?
- 电梯系列——OO Unit2分析和总结
- Python通过pip方式安装第三方模块的两种方式
- Kafka 0.10 Coordinator概述
- Unity读取Excel文件(附源代码)
- Spring Boot设置值:分别用@ConfigurationProperties和@Value给属性设值及其区别
- day23-类的封装
- eclipse 中springboot2.0整合jsp 出现No Java compiler available for configuration options compilerClassName