自定义标签出现空指针错误
各位大神求助,在用自定义标签时,出现空指针错误,报错如下:org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:538)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause
java.lang.NullPointerException
org.apache.jsp.page.defineTag_jsp._jspInit(defineTag_jsp.java:30)
org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:164)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
action代码:
package action;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloAction extends TagSupport {
/**
*
*/
private static final long serialVersionUID = -6010459496748855933L;
private String name = null;
public void setName(String name) {
this.name = name;
}
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.print("Hello!"+name);
} catch (Exception e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
}
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>msp</short-name>
<tag>
<name>out</name>
<tag-class>org.msp.action.HelloAction</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
JSP代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/tlds/hello.tld" prefix="m"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
测试标签:<m:out name="自定义标签"/>
</body>
</html>
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>自定义标签 空指针 --------------------编程问答-------------------- package action;
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>tag</display-name>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tlds/hello.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/hello.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>page/defineTag.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
<tag-class>org.msp.action.HelloAction</tag-class>
你包名里没有这个红色的吧。 --------------------编程问答-------------------- 我把包名写错了,不过改回来还是一样报空指针。
补充:Java , Web 开发