3

我正在编写一个jsp页面,该页面使用 ascriptlet从控制器调用方法,该控制器使用来自 DAO 的方法,该方法执行存储在与该 DAO 对应的模型中的命名查询。我希望这个控制器方法返回一个ArrayList我可以迭代的方法来检索它的值以显示在页面上。
jsp到达方法调用时,它给了我一个NPE控制器方法,特别是在调用查询方法的行上。更令人烦恼的是,我在页面前面调用了一个类似的方法,并且没有出现异常。我的小组一直在使用带有 hibernate 的 Spring MVC 框架来处理我们的映射。
我试图删除代码的非必要部分,同时保留可能指向解决方案的内容。如果您需要更多信息,请告诉我,我会尽快更新。
(我计划在未来学习避免使用 scriptlet,因为到目前为止我读过的所有问题都告诉我要避免使用它们。)代码如下: ReportingController 方法:

public ArrayList<String> weekWorkPrints(User user, CurrentWeek week) {
    ArrayList<UserTime> timeList = userTimeDao
            .getPunchesForUserAscendingByTime(user);
    ArrayList<String> prints = new ArrayList<String>();
    prints.add(utc.convertPeriodToPrint(utc.timeWorkedForADay(
            week.getSunday(), timeList)));
    return prints;
}

调用查询的 UserTimeDao 方法:

@SuppressWarnings("unchecked")
@Transactional
public ArrayList<UserTime> getPunchesForUserAscendingByTime(User passedUser) {

    Query loginQuery = em
            .createNamedQuery("fetchAllUserTimesForUserAscend");
    loginQuery.setParameter("user", passedUser);

    return (ArrayList<UserTime>) loginQuery.getResultList();
}

UserTime 模型中的命名查询:

    @NamedQueries({
    @NamedQuery(name = "fetchAllUserTimes", query = "SELECT ut FROM UserTime ut"),
    @NamedQuery(name = "fetchAllUserTimesForUserDesc", query = "SELECT ut FROM UserTime ut WHERE ut.user = :user ORDER BY ut.userClockIn DESC"),
    @NamedQuery(name = "fetchAllUserTimesForUserAscend", query = "SELECT ut FROM UserTime ut WHERE ut.user = :user ORDER BY ut.userClockIn")})

JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="models.*"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="models.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="controllers.ReportingController"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="userDao" class="daos.UserDao" scope="request" />
<jsp:useBean id="reportControl" class="controllers.ReportingController"
scope="request" />
<jsp:useBean id="currentWeekControl"
class="controllers.CurrentWeekController" scope="session" />
<jsp:useBean id="userTimeDao" class="daos.UserTimeDao" scope="request" />
<html>
<head>

<link rel="stylesheet" type="text/css" href="css/global.css" />
<link rel="stylesheet" type="text/css" href="css/reporting.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Administrator - Reporting</title>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h1>Administrator Home</h1>
        <%
            User loginUser = (User) session.getAttribute("loginUser");
        %>
        <p>
            Welcome,
            <%=loginUser.getUserName()%>!
        </p>
    </div>
<!-- ---- -->
                <table id="loggedTimeTable">
                    <%
                        CurrentWeek currentWeek = (CurrentWeek) session
                                .getAttribute("currentWeek");
                    %>
                    <!-- --- -->
                    <%
                        for (User iteratedUser : userDao.getAlphabeticalUsers()) {
                            ArrayList<String> weekPrintouts = reportControl.weekWorkPrints(
                                    iteratedUser, currentWeek);
                    %>
                    <tr>
                        <td><%=iteratedUser.getLastName()%>, <%=iteratedUser.getFirstName()%>
                        </td>
                        <td><%=weekPrintouts.get(0)%></td>
                    </tr>
                    <%
                        }
                    %>
                </table>

空指针异常:

2012-08-19 21:21:02.315::WARN:  Nested in org.apache.jasper.JasperException: java.lang.NullPointerException: java.lang.NullPointerException
    at controllers.ReportingController.weekWorkPrints(ReportingController.java:106)
    at org.apache.jsp.jsp_002dpages.Reporting_jsp._jspService(org.apache.jsp.jsp_002dpages.Reporting_jsp:196)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:93)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:470)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:285)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:155)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
4

1 回答 1

3

您的问题是您的 useBean for reportControl 只是创建了一个全新的 ReportingController,它没有注入 UserDao。

您尝试做的事情在 Spring MVC 中根本行不通,也不应该。MVC 的目标是分离模型、视图和控制器,但您试图在视图中创建控制器的实例。

相反,您拥有的返回 JSP 名称的控制器方法应该首先调用您的 weekWorkPrints 方法并将返回的 List 放入模型中,以便 JSP 可以遍历它。

于 2012-08-20T04:36:09.053 回答