| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1662 人关注过本帖
标题:java web开发中自定义标签内容返回Exception的问题
取消只看楼主 加入收藏
WangZPeng
Rank: 1
来 自:安徽蚌埠
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-9-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
java web开发中自定义标签内容返回Exception的问题
本人新学web开发,就遇到了这个问题,求各位大神帮忙

taglib程序代码如下:

程序代码:
package szy.shopping.taglib;

import  java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import szy.shopping.entity.Goods;

public class SearchGoodsTag extends TagSupport{

    @Override
    public void setPageContext(PageContext pageContext) {
        System.out.println("set pageContext");
        PageContext ctx = pageContext;
    }
    
    
    public int doStartTag() throws JspException {
        
        List<Goods> goodsList=(List<Goods>)pageContext.getRequest().getAttribute("goodsList");
        try {
            pageContext.getOut().print("<br><br><br>");
            if(goodsList.isEmpty()){
                pageContext.getOut().print("找不到和您的查询 相符的商品。");
            }
            else {
                pageContext.getOut().print("◆ 共找到<font color='red'>"+goodsList.size()+"</font>种符合您查询的商品<br><br>");
                pageContext.getOut().print("<table border='0'>");
                for (Goods goods : goodsList) {
                    pageContext.getOut().print("<tr>");
                    pageContext.getOut().print("<td>");
                    pageContext.getOut().print("<a href='GoodsDetailsServlet?goodsID="+goods.getGoodsID()+"'><font size='+1' color='blue'>&nbsp;&nbsp;·"+goods.getGoodsName()+"</font></a>");
                    pageContext.getOut().print("</td>");
                    pageContext.getOut().print("</tr>");
                }
                pageContext.getOut().print("</table>");
            }
            
        } catch (IOException e) {
            System.err.println("SearchGoodsTag.doEndTag ERROR ## "+e.getMessage());
            e.printStackTrace();
        }
        
        return TagSupport.EVAL_BODY_INCLUDE;
    }

  public int doEndTag() throws JspException {
      System.out.println("do end tag");
      return TagSupport.EVAL_PAGE;
  }
}


tld文件:

程序代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java. <tlibversion>1.0</tlibversion>
    <jspversion>1.2</jspversion>
    <shortname>my</shortname>
    <uri>http:// 
        <tag>
        <name>searchResult</name>
        <tagclass>szy.shopping.taglib.SearchGoodsTag</tagclass>
        <bodycontent>JSP</bodycontent>
    </tag>
</taglib>


jsp页面代码:

程序代码:
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib uri="http:// prefix="mytags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www. http-equiv="Content-Type" content="text/html; charset=GBK">
<title>在线购物系统</title>
<link href="include/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
         <tr>
            <td align="left" valign="top">
              <div class="title">查询结果</div>
              <mytags:searchResult></mytags:searchResult>
            </td>
         </tr>
   </table>
</body>
</html>
   


返回的Exception如下:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /searchResult.jsp at line 30

27:             </td>
28:             <td align="left" valign="top">
29:               <div class="title">查询结果</div>
30:               <mytags:searchResult></mytags:searchResult>
31:             </td>
32:           </tr>
33:         </table>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    szy.shopping.servlet.customer.SearchGoodsServlet.doPost(SearchGoodsServlet.java:49)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
    szy.shopping.taglib.SearchGoodsTag.doStartTag(SearchGoodsTag.java:23)
    org.apache.jsp.searchResult_jsp._jspx_meth_mytags_005fsearchResult_005f0(searchResult_jsp.java:140)
    org.apache.jsp.searchResult_jsp._jspService(searchResult_jsp.java:102)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    szy.shopping.servlet.customer.SearchGoodsServlet.doPost(SearchGoodsServlet.java:49)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
搜索更多相关主题的帖子: package web开发 public import java 
2015-09-25 00:26
WangZPeng
Rank: 1
来 自:安徽蚌埠
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-9-25
收藏
得分:0 
.....

黑夜给了我黑的眼睛,我却用它来寻找光明!
2015-09-25 14:40
WangZPeng
Rank: 1
来 自:安徽蚌埠
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-9-25
收藏
得分:0 
如何实例化呀,在哪进行实例化?对哪个对象实例化?
没有系统学过java

黑夜给了我黑的眼睛,我却用它来寻找光明!
2015-09-25 17:52
WangZPeng
Rank: 1
来 自:安徽蚌埠
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-9-25
收藏
得分:0 
帮忙看一下,我觉得SearchGoodsTag.java中只有goodsList这个变量需要实例化,但是不知道怎么实例化,谢谢
程序代码:
package szy.shopping.taglib;

import  java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import szy.shopping.entity.Goods;

public class SearchGoodsTag extends TagSupport{

    @Override
    public void setPageContext(PageContext pageContext) {
        System.out.println("set pageContext");
        PageContext ctx = pageContext;
    }
    
    
    public int doStartTag() throws JspException {
        List<Goods> goodsList=(List<Goods>)pageContext.getRequest().getAttribute("goodsList");
        
        try {
            pageContext.getOut().print("<br><br><br>");
            if(goodsList.isEmpty()){
                pageContext.getOut().print("找不到和您的查询 相符的商品。");
            }
            else {
                pageContext.getOut().print("◆ 共找到<font color='red'>"+goodsList.size()+"</font>种符合您查询的商品<br><br>");
                pageContext.getOut().print("<table border='0'>");
                for (Goods goods : goodsList) {
                    pageContext.getOut().print("<tr>");
                    pageContext.getOut().print("<td>");
                    pageContext.getOut().print("<a href='GoodsDetailsServlet?goodsID="+goods.getGoodsID()+"'><font size='+1' color='blue'>&nbsp;&nbsp;·"+goods.getGoodsName()+"</font></a>");
                    pageContext.getOut().print("</td>");
                    pageContext.getOut().print("</tr>");
                }
                pageContext.getOut().print("</table>");
            }
            
        } catch (IOException e) {
            System.err.println("SearchGoodsTag.doEndTag ERROR ## "+e.getMessage());
            e.printStackTrace();
        }
        
        return TagSupport.EVAL_BODY_INCLUDE;
    }

  public int doEndTag() throws JspException {
      System.out.println("do end tag");
      return TagSupport.EVAL_PAGE;
  }
}

黑夜给了我黑的眼睛,我却用它来寻找光明!
2015-09-25 18:02
快速回复:java web开发中自定义标签内容返回Exception的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032050 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved