0

我的 JSP 没有运行一个自定义标签。该标记应该在我的数据库上运行查询并将这些值返回给我的 JSP,但是当我知道数据库中存在非零值这一事实时,我的页面中会出现零。我已经在调试模式下运行了应用程序,并且由于某种原因没有调用该标记,但由于某种原因我似乎得到了 NullPointerException,即使该标记存在。这是我的 JSP 的相关部分。此部分仅在存在 cookie 时出现。

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<td><myTag1:poll1 /> <c:choose>
        <c:when test="${foundCookiePoll1 == true}">
            <table>
                <tr>
                    <td><b><i>Poll #1 -- </i></b>Would you like to have a
                        30-year reunion in 2016?<br></td>
                </tr>
                <tr>
                    <td><b>Yes</b></td>
                    <td>&nbsp;&ndash;&nbsp;<c:out value='${poll1Yes}' /><br />                      </td>
                </tr>
                <tr>
                    <td><b>No</b></td>
                    <td>&nbsp;&ndash;&nbsp;<c:out value='${poll1No}' /><br />                       </td>
                </tr>
            </table>
        </c:when>

这是我的标签。

public void doTag(HttpServletRequest request)
        throws JspException, IOException {
    PageContext pageContext = (PageContext) getJspContext();
    HttpSession session = request.getSession(true);
    ServletContext servletContext = session.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);
    Poll1DAO poll1DAO = (Poll1DAO) wac.getBean("poll1DAO");

    pageContext.setAttribute("foundCookiePoll1", cookieFound());
    if (cookieFound()) {
        HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
        pageContext.setAttribute("poll1Yes", (int) poll1Votes.get("yes"));
        pageContext.setAttribute("poll1No", (int) poll1Votes.get("no"));
    }
}

private boolean cookieFound() {
    PageContext pageContext = (PageContext) getJspContext();
    HttpServletRequest request = (HttpServletRequest) pageContext
            .getRequest();
    Cookie[] cookies = request.getCookies();

    if (cookies == null) {
        return false;
    }

    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals("poll1")) {
            return true;
        }
    }

    return false;
}

这是我的这个标签的 tld.xml。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>Poll1Tag</short-name>
    <uri>poll1</uri>
    <tag>
        <name>poll1</name>
        <tag-class>com.tags.Poll1Tag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>
4

1 回答 1

-1

这可能不是执行此操作的最佳方式,但在页面的控制器方法中执行此操作。

    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("poll1")) {
                HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
                model.addAttribute("poll1Yes", (int) poll1Votes.get("yes"));
                model.addAttribute("poll1No", (int) poll1Votes.get("no"));
            }
        }
    }

在 JSP 中,像这样引用变量。

<%= poll1Yes %>

像这样获取变量。

int poll1Yes = (Integer) request.getAttribute("poll1Yes");
int poll1No = (Integer) request.getAttribute("poll1No");
于 2013-06-03T02:28:38.670 回答