一个jsp标签问题,谢谢!

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

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




ecc:div
eccdiv
/ecc:div


[/code]
标签的tld
[code="java"]
<?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">

1.0
1.2
ecc wall ext
wap

div
com.ecc.tag.Div
JSP

className
false


style
false



[/code]

标签类:
[code="java"]
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";
}

}

[/code]

[code="java"]
标签父类:

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;
} 

}
[/code]

[code="java"]
标签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;
}

... ...
[/code]

就是在getUserAgent() 方法中pageContext.getRequest()时会空指针异常。
[code="java"]
java.lang.NullPointerException
at com.ecc.tag.BaseTag.getUserAgent(BaseTag.java:34)
at com.ecc.tag.BaseTag.(BaseTag.java:38)
at com.ecc.tag.StyledBaseTag.(StyledBaseTag.java:18)
at com.ecc.tag.Div.(Div.java:7)
... ...
[/code]
我直接用标签类扩展了tagsupport类,在标签类中去request一切都正常,百思不得其解,请各位帮忙指教!谢谢!

1个回答

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

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐