自定义标签的开发步骤
1.编写标签处理器
1).传统标签开发。需要实现javax.servlet.jsp.tagext.Tag接口
2).简单标签开发。需要实现javax.servlet.jsp.tagext.SimpleTag接口
2..编写一个类,实现javax.servlet.jsp.tagext.SimpleTag,或者继承
3.编写标签库描述符文件
在WEB-INF目录下建立一个扩展名为tld的xml文件。
<?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">
<tlib-version>1.0</tlib-version>
<short-name>hhh</short-name>
<uri>http://www.xxx.com/jsp/tags</uri>
<tag>
<name>showRemoteIp</name>
<tag-class>com.xxx.ShowRemoteIpSimpleTag</tag-class>
<!-- 说明标签的主体内容 :没有就写empty-->
<body-content>empty</body-content>
</tag>
</taglib>
<body-content>empty</body-content>指定标签体的类型
1).empty:不能设置标签体
2).JSP:标签体可以为任意的JSP元素
3).scriptless:标签体可以包含除了JSP脚本元素之外的任意JSP元素
4).tagdependent :标签体内容不进行处理
4.在jsp页面导入和使用自定义标签
简单标签
1.定义标签类
public class Demo1SimpleTag extends SimpleTagSupport {
//主体内容不需要显示,就什么都不写
public void doTag() throws JspException, IOException {
}
}
2.配置tld
<?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">
<tlib-version>1.0</tlib-version>
<short-name>hhh</short-name>
<uri>http://www.xxx.com/jsp/tags</uri>
<tag>
<name>xihuan</name>
<tag-class>com.xxx.tag.ShowRemoteIpSimpleTag</tag-class>
<!-- 因为要包含所以用scriptless-->
<body-content>scriptless</body-content>
</tag>
3.jsp
<body>
我喜欢
<hhh:xihuan>某某</itheima:demo1>//此处某某不显示
</body>
-----------------------------------------------------------
1.显示主题
public void doTag() throws JspException, IOException {
// JspFragment jf = getJspBody();//代表标签的主体内容
// PageContext pc = (PageContext)getJspContext();
// JspWriter out = pc.getOut();
// jf.invoke(out);
getJspBody().invoke(null);//如果是空默认为JspWriter
2.标签后的内容不显示
<!-- 因为不是标签体所以用empty-->
<body-content>empty</body-content>
public void doTag() throws JspException, IOException {
throw new SkipPageException();
}
3.重复输出
private int count;//自动转换
public void setCount(int count) {
this.count = count;
}
public void doTag() throws JspException, IOException {
for(int i=0;i<count;i++){
getJspBody().invoke(null);
}
<attribute>
<name>count</name>
<required>true</required>
<!-- rt:RunTime expr:Expression value 是否支持表达式:java表达式或EL表达式-->
<rtexprvalue>true</rtexprvalue>
</attribute>
<body>
<% int i = 3; %>
<xihuan:demo3 count="${1+4}">
某某
</xihuan:demo3>
</body>
4.大小写的转换
public void doTag() throws JspException, IOException {
StringWriter sw = new StringWriter();
//得到主体内容
JspFragment jf =getJspBody();
jf.invoke(sw);
//变为大写后输出
String content = sw.toString();
content = content.toUpperCase();
PageContext pc = (PageContext)getJspContext();
pc.getOut().write(content);
}
<tag>
<name>showRemoteIp</name>
<tag-class>com.xxx.tag.ShowRemoteIpSimpleTag</tag-class>
<!-- 说明标签的主体内容 :没有就写empty-->
<body-content>empty</body-content>
</tag>
public class ShowRemoteIpSimpleTag extends SimpleTagSupport {
public ShowRemoteIpSimpleTag(){
System.out.println("实例化了");
}
public void doTag() throws JspException, IOException {
PageContext pc = (PageContext)getJspContext();
String ip = pc.getRequest().getRemoteAddr();
pc.getOut().write(ip);
}
}
<body>
您的IP是:
<itheima:showRemoteIp/>
</body>