| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1117 人关注过本帖
标题:关于bodyContent为空值的问题
取消只看楼主 加入收藏
liugy52
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-10-1
收藏
 问题点数:0 回复次数:1 
关于bodyContent为空值的问题

标签处理类代码:
---------------------------------------------------------------------------------
package cn.tag;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class MytagForEach extends BodyTagSupport {

private static final long serialVersionUID = 1L;

protected String value = null;
protected int count = 0;

public MytagForEach() {
super();
}

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setCount(int count) {
this.count = count;
}

public int getCount() {
return count;
}

public int doStartTag() throws JspTagException {
if (count > 0) {
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
}

public int doEndTag() throws JspTagException {
return EVAL_PAGE;
}

public int doAfterBody() throws JspTagException {
if (count-- >= 1) {
try {
JspWriter out = bodyContent.getEnclosingWriter();
out.println(bodyContent.getString() + value);
out.println("<br>");
bodyContent.clearBody();
} catch (IOException ex) {
System.out.println("Error in RepeatTag: " + ex);
}
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
}

public void doInitBody() throws JspTagException {}
}



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/web-jsptaglibrary_2_0.xsd"
version="2.0">

<description>mytag 1.1 print library</description>
<display-name>mytag tag library</display-name>
<tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<uri>http://www.tag.com/mytag</uri>

<tag>
<description>An instance of 'forEach' with tag body and attribute</description>
<name>forEach</name>
<tag-class>cn.tag.MytagForEach</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>


JSP文件:
----------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ taglib prefix="mt" uri="http://www.tag.com/mytag"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<h3>调用mytag标签库中的forEach标签</h3>
<mt:forEach value="Johnson" count="5">Hello</mt:forEach>
</body>
</html>



错误信息:
-------------------------------------------------------------------
500 Servlet Exception
java.lang.NullPointerException
at cn.tag.MytagForEach.doAfterBody(MytagForEach.java:52)
at _jsp._foreach__jsp._jspService(foreach.jsp:11)
at com.caucho.jsp.JavaPage.service(JavaPage.java:60)
at com.caucho.jsp.Page.pageservice(Page.java:570)
at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:179)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:173)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:274)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:511)
at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:520)
at com.caucho.util.ThreadPool.run(ThreadPool.java:442)
at java.lang.Thread.run(Thread.java:595)


--------------------------------------------------------------------------------
Resin-3.0.23 (built Mon, 22 Jan 2007 02:16:07 PST)


为什么bodyContent的值是空的???

搜索更多相关主题的帖子: import bodyContent servlet javax jsp 
2007-08-15 16:53
liugy52
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-10-1
收藏
得分:0 
public int doAfterBody() throws JspTagException {
if (count-- >= 1) {
try {
JspWriter out = bodyContent.getEnclosingWriter(); //////// 在这一行发生了错误 ///////
.... .... ....
....
}

我在大家热情的帮助下进步,谢谢你们!
2007-08-15 16:56
快速回复:关于bodyContent为空值的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018507 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved