- 定义一个普通java类
- 配置一个xml的带后缀 .tld的文件
- 在jsp中调用
表2-1 定义带有属性的标签 方式一
无标签体
定义标签类
package cn.xinhua.tag;
public class attributetag extends TagSupport{
private String format;//属性
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public int doStartTag() throws JspException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
this.pageContext.getOut().write(sdf.format(new Date()));
} catch (IOException e) {
throw new RuntimeException(e);
}
return TagSupport.SKIP_BODY;//跳过标签体
}
}
定义带有属性的标签
注册标签
attribute
cn.xinhua.tag.attributetag
empty(表示标签体内容为空)
format
true(表示此值必须设置)
true(表示可以接受表达式的值)
定义带有属性的标签
在jsp中引用
<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
<%–@ taglib prefix=“test” uri="/WEB-INF/testtab.tld"–%>
<%@ taglib prefix=“testtab” uri=“http://www.xinhua.com”%>
<%@ taglib prefix=“attributetag” uri=“http://www.xinhua.com”%>//引入
<%–@ taglib prefix=“test” uri=“testtab”–%>
<title></title>