0

我在 Spring JSP 中运行自定义标记。我收到以下错误消息。

java.lang.NullPointerException
    com.dao.Poll1DAO.getVotes(Poll1DAO.java:27)
    com.tags.Poll1Tag.doTag(Poll1Tag.java:23)
    org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspx_meth_myTag1_005fpoll1_005f0(PollPage_jsp.java:602)
    org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspService(PollPage_jsp.java:195)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是 Poll1DAO.getVotes()。

public HashMap<String, Object> getVotes() {
    // following line is line 27, referenced in error message
    return (HashMap<String, Object>) this.getJdbcTemplate().queryForMap(
            "select yes, no from poll1 where id = 1");
}

这是 Poll1Tag.doTag()。

Poll1DAO poll1DAO = new Poll1DAO();

public void doTag() throws JspException, IOException {
    PageContext pageContext = (PageContext) getJspContext();
    boolean foundCookie = cookieFound();

        pageContext.setAttribute("foundCookiePoll1", foundCookie);
        if (foundCookie) {
            // following line is line 23, referenced in error message
            HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
            pageContext.setAttribute("poll1Yes", (int) poll1Votes.get("yes"));
            pageContext.setAttribute("poll1No", (int) poll1Votes.get("no"));
        }
}
4

1 回答 1

0

如果您使用 IoC 容器,例如 Spring 的应用程序上下文,您永远不应该自己创建托管 bean 实例。您总是需要将实例从容器中拉出 - 即ApplicationContext. 否则,它们将不会使用必要的依赖项进行初始化。

现在您应该问的问题是 - 我如何ApplicationContext在 JSP 标记类中获取实例。答案很简单——看看标准 Spring 标签是如何做到的(我的意思是——去检查那个链接)。

// Example on how to obtain bean from JSP tag (ignoring context hierarchy)
WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).
        getBeansOfType(Poll1DAO.class).values().iterator().next();
于 2013-05-19T17:13:58.717 回答