jsp 自定义标签【继承TagSupport类】【在 tld描述 中声明 代码段变量】 简单例子四
2010-07-01 10:09
399 查看
就是在jsp页面中调用变量如<%=a%>
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);
注意:这里的key,就是tld中,声明的变量的名称,tld文档就是通过这个key进行关联的
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。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>
注意:这段是前面介绍的,参数值的传入,传入一个计算,范围的变量scope
<attribute>
<name>scope</name>
<required>true</required>
<type>java.lang.Integer</type>
<description>return scope double</description>
</attribute>
注意:这段是声明jsp文件中调用的,变量的名称,与pageContext,进行,关联
<variable>
<name-given>randValue</name-given> 注意:必须与pageContext的,key相同
<variable-class>java.lang.Double</variable-class>
<scope>AT_END</scope> 范围
<declare>true</declare> 是否,是已经定义过的,
<description>var in jsp</description>
</variable>
</tag>
</taglib>
3。页面中调用:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="test" uri="/test-1.0"%>
TestC:<test:getRand scope="100"></test:getRand>
<%=randValue %>
4。关于,variable属性,的scope ,和declare的说明
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);
注意:这里的key,就是tld中,声明的变量的名称,tld文档就是通过这个key进行关联的
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。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>
注意:这段是前面介绍的,参数值的传入,传入一个计算,范围的变量scope
<attribute>
<name>scope</name>
<required>true</required>
<type>java.lang.Integer</type>
<description>return scope double</description>
</attribute>
注意:这段是声明jsp文件中调用的,变量的名称,与pageContext,进行,关联
<variable>
<name-given>randValue</name-given> 注意:必须与pageContext的,key相同
<variable-class>java.lang.Double</variable-class>
<scope>AT_END</scope> 范围
<declare>true</declare> 是否,是已经定义过的,
<description>var in jsp</description>
</variable>
</tag>
</taglib>
3。页面中调用:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="test" uri="/test-1.0"%>
TestC:<test:getRand scope="100"></test:getRand>
<%=randValue %>
4。关于,variable属性,的scope ,和declare的说明
相关文章推荐
- jsp 自定义标签【继承TagSupport类】【继承TagExtraInfo类 声明 代码段变量】 简单例子五
- 【Java.Web】JSP —— 标签库taglib —— 自定义JSP标签 —— 标签库描述文件 TLD,发布
- java:jsp: 一个简单的自定义标签 tld
- jsp 自定义标签【EL表达式定义】 简单例子一
- JSP 自定义标签:简单标签
- JSP自建标签_1_TLD文件描述
- JSP自建标签_3_简单标签自定义_为标签定义属性
- JSP自定义标签——简单标签(1)
- JSP自定义标签开发+TLD文件元素详解
- JSP自定义标签之一:简单实例
- 自定义JSP标签:TagSupport类的生命周期
- 自定义JSP中的Taglib标签之三简单仿JSTL中带标签体的ForEach循环
- [教程]自定义JSP中的Taglib标签之三简单仿JSTL中带标签体的ForEach循环
- JSP自定义标签之简单标签入门
- JSP 自定义标签之一 简单实例
- JSP自定义标签(一):自定义JSP简单标签
- JSP----自定义标签---简单标签
- JAVA_WEB之 Jsp简单的自定义标签
- 关于jsp页面自定义jstl标签简单项目配置