我在标签的超类中在pagecontext中取request时会报空指针异常,请各位帮我看看:
[code="java"]
简单的测试标签页面,就一个div
<%@page language="java" contentType="text/xml;charset=UTF-8" %>
<%@taglib prefix="ecc" uri="wap" %>
标签类:
[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一切都正常,百思不得其解,请各位帮忙指教!谢谢!
[size=medium]34行呢???
怎么没贴出来
可能是这样的,一般tag中的空指针都是这么回事的:
pageContext在页面调用doEndTag之后就消亡了
所以你在其他方法调用的时候就会出现空指针异常
我是怎么解决的
你可以作一个容器,把tag对象展示持久化一段时间
这样你的pageContext就不会消亡了
ps:
我现在写的东西都这么写的,自己写一个容器管理他们的生命周期[/size]