我在标签的超类中在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一切都正常,百思不得其解,请各位帮忙指教!谢谢!