gpt4 book ai didi

java - 使用 JPQL 命名查询调用方法时 .jsp 出现 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:19 25 4
gpt4 key购买 nike

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

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)

最佳答案

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

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

相反,您拥有的返回 JSP 名称的 Controller 方法应该首先调用您的 weekWorkPrints 方法并将返回的列表放入模型中,以便 JSP 可以循环访问它。

关于java - 使用 JPQL 命名查询调用方法时 .jsp 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com