一个jsp标签问题,谢谢!

悬赏:5 发布时间:2008-07-23 提问人:djhyoo (初级程序员)

我在标签的超类中在pagecontext中取request时会报空指针异常,请各位帮我看看:

简单的测试标签页面,就一个div
<%@page language="java" contentType="text/xml;charset=UTF-8" %>
<%@taglib prefix="ecc" uri="wap" %>
<html>
<head>
</head>
<body>
	<ecc:div>
	eccdiv
	</ecc:div>
</body>
</html>

标签的tld
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
		PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
		"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>ecc wall ext</short-name>
	<uri>wap</uri>
	<tag>
		<name>div</name>
		<tag-class>com.ecc.tag.Div</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>className</name>
			<required>false</required>
		</attribute>
		<attribute>
			<name>style</name>
			<required>false</required>
		</attribute>
	</tag>
</taglib>


标签类:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

public class Div extends StyledBaseTag{

	public int doStartTag() throws JspException {
		if(isPreferWML())
			return EVAL_BODY_INCLUDE;
		return super.doStartTag();
	}

	public int doEndTag() throws JspException {
		if(isPreferWML())
			return EVAL_PAGE;
		return super.doEndTag();
	}

	protected String getTagString() {
		return "div";
	}
	
}




标签父类:

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

public abstract class StyledBaseTag extends BaseTag {
	protected String style;
	protected String className;

	protected abstract String getTagString();

	public int doStartTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			out.write("<"+getTagString());
			if (isPreferXHTML()) {
				if (style != null)
					out.write(" style=\"" + style + "\"");
				if (className != null)
					out.write(" class=\"" + className + "\"");
			}
			out.write(">");
		} catch (IOException ioe) {
			throw new JspException("Error in Tag : "+this.getClassName()+" " + ioe);
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {

		try {
			JspWriter out = pageContext.getOut();
			out.write("</"+getTagString()+">");
		} catch (IOException ioe) {
			throw new JspException("Error in Tag : "+this.getClassName()+" " + ioe);
		}
		return (EVAL_PAGE); // Continue with rest of JSP page
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	} 
}


标签base:
public abstract class BaseTag extends TagSupport {

	protected HttpSession getSession() {
		return pageContext.getSession();
	}

	public HttpServletResponse getResponse() {
		return (HttpServletResponse) pageContext.getResponse();
	}

	public String getUserAgent() {
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		return EccUtil.getUA(request);
	}

	public WurflDevice wd = WurflDevice.getDeviceByUserAgent(getUserAgent());

	public String getPreferMarkup() {
		String markup = "wml";
		try {
			markup = wd.getCapability("preferred_markup");
		} catch (CapabilityNotSupportedException e) {
			e.printStackTrace();
		}
		return markup;
	}
... ...



就是在getUserAgent() 方法中pageContext.getRequest()时会空指针异常。
java.lang.NullPointerException
	at com.ecc.tag.BaseTag.getUserAgent(BaseTag.java:34)
	at com.ecc.tag.BaseTag.<init>(BaseTag.java:38)
	at com.ecc.tag.StyledBaseTag.<init>(StyledBaseTag.java:18)
	at com.ecc.tag.Div.<init>(Div.java:7)
... ...

我直接用标签类扩展了tagsupport类,在标签类中去request一切都正常,百思不得其解,请各位帮忙指教!谢谢!

采纳的答案

2008-07-24 aidiyuxin (资深程序员)

34行呢???
怎么没贴出来
可能是这样的,一般tag中的空指针都是这么回事的:
pageContext在页面调用doEndTag之后就消亡了
所以你在其他方法调用的时候就会出现空指针异常

我是怎么解决的
你可以作一个容器,把tag对象展示持久化一段时间
这样你的pageContext就不会消亡了


ps:
我现在写的东西都这么写的,自己写一个容器管理他们的生命周期

提问者对于答案的评价:
学到了另外一种解决方法,多谢!!没有多的分了,见谅!

:-)