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 に答える