0。前提:
TagData:描述标签属性相关信息的类
VariableInfo :描述代码段,jsp中,变量的一些,定义信息
1。标签代码实现类:【与上个例子,解释一样,不在赘言】
package taglib;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
/**
*
* @author zwc
*
*/
@SuppressWarnings("serial")
public class RandUtils extends TagSupport {
private Logger logger = Logger.getLogger(RandUtils.class);
private int scope;
@Override
public int doEndTag() throws JspException {
if(scope == 0){
scope = 1;
}
double d = Math.random() * scope;
pageContext.setAttribute("randValue", d);
logger.info("scope: " + scope + " rand: " + d);
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
public int getScope() {
return scope;
}
public void setScope(int scope) {
this.scope = scope;
}
}
2。定义自己TagExtraInfo【tei】的类;
package taglib;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.ValidationMessage;
import javax.servlet.jsp.tagext.VariableInfo;
import org.apache.log4j.Logger;
/**
*
* @author zwc
*
*/
public class RandUtilsTagExtraInfo extends TagExtraInfo {
private Logger logger = Logger.getLogger(RandUtilsTagExtraInfo.class);
@Override
public VariableInfo[] getVariableInfo(TagData data) {
VariableInfo vi = new VariableInfo("randValue", "java.lang.Double", true, VariableInfo.AT_END);
注意:这里的定义,是不是,跟,原先的,描述文件里的,定义,内容是否很相似,对了,就是为了替换那段的另外一种表现形式
return new VariableInfo[]{vi};
}
}
3。tld描述文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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>
<uri>/test-1.0</uri>
<tag>
<name>getRand</name>
<tag-class>taglib.RandUtils</tag-class>
<tei-class>taglib.RandUtilsTagExtraInfo</tei-class> 注意:这里,声明,TagExtraInfo的类的,地方
<attribute>
<name>scope</name>
<required>true</required>
<type>java.lang.Integer</type>
<description>return scope double</description>
</attribute>
<!--
<variable>
<name-given>randValue</name-given>
<variable-class>java.lang.Double</variable-class>
<scope>AT_END</scope>
<declare>true</declare>
<description>var in jsp</description>
</variable>
-->
注:因为,上面的那个类,已经什么了,相关的关于,变量的信息,所以,这里的,就可以注释了,这两部分,是等价的关系。
</tag>
</taglib>
4。页面调用:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="test" uri="/test-1.0"%>
TestC:<test:getRand scope="0"></test:getRand>
<%=randValue %>
5。后记
TagExtraInfo 还有一些,验证功能如重写 isValid(TagData data)方法,,等,可以参考一些文档。