java自定义标签 map_java(jsp)的自定义标签

ppackage cn.tag;

import java.io.IOException;

import java.util.Map;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.BodyTagSupport;

public class TestTag extends BodyTagSupport {

private static final long serialVersionUID = -5663050580910865400L;

/**

* 可以在这里进行一些初始化的工作

*/

public int doStartTag() throws JspException {

System.out.println("----doStartTag()----");

JspWriter out = this.pageContext.getOut();

try {

out.println("before");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return this.EVAL_BODY_INCLUDE;

}

/**

* 核心处理标签的地方,业务逻辑在此实现,如:分页,根据参数返回给前台的值等等

*/

public int doEndTag() throws JspException {

JspWriter out = this.pageContext.getOut();

System.out.println("----doEndTag()----");

/**

* 获取参数

*/

Map map=this.pageContext.getRequest().getParameterMap();

if(map==null){

System.out.println("map is null");

}else{

System.out.println("map not null");

System.out.println(map.size());

}

try {

out.println("end");

} catch (IOException e) {

System.out.println(e.getMessage());

e.printStackTrace();

}

return super.doEndTag();

}

}

定义testTag.tld,貌似有些项是必需的,如下所示:

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">

测试

1.1

tt

/testTag

test

cn.tag.TestTag

JSP

测试页面,关键是要引入testTag.tld文件:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'test.jsp' starting page

测试

显示结果:

控制台:

----doStartTag()----

----doEndTag()----

map not null

页面:

before 测试end

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-04-09 11:52

浏览 1126

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值