使用自定义标签Demo
1.创建一个dynamic web project 并生成web.xml 文件
2.在WEB-INF下创建一个 .tld 文件 (自定义标签的一些属性)
xml 前缀信息通用
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>helloWorldTag</short-name>
<tag>
<name>hello</name>
<tag-class>com.liang.customtag.CusotmTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3.创建一个class 类继承 SimpleTagSupport 重写doTag()
package com.liang.customtag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class CusotmTag extends SimpleTagSupport{
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.print("hello coustom Tag!");
}
}
4.创建一个jsp 页面 引入 自定义标签
前缀自定义一个即可
<%@ taglib prefix=“custom” uri="/WEB-INF/custom.tld" %>
<custom:hello/>//在页面就会打印出 out.print("hello coustom Tag!");中的内容
JSTL核心标签库
1.项目导入lib 库 jstl.jar和standard.jar
在jsp 页面引入 JSTL
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>