Hibernate Spring Object Wrapper不传递值
我正在努力创造一种可以插入多个电表的读数的表格。 我构建了一个包含所有值的包装器。 所有看起来都不错,直到我提交读数,因为我得到java.lang.NullPointerException。
这是控制器的一部分:
@RequestMapping("/registerEnergyReadings")
public ModelAndView registerEnergyReadings(
@ModelAttribute("energyReadingWrapper") EnergyReadingWrapper, energyReadingWrapper, BindingResult result) {
// loading available meters from DB:
List<EnergyMeters> energyMeters= meterService.getMeters();
List<EnergyReading> readings= new ArrayList<EnergyReading>();
//preparing the wrapper
for (int i = 0; i < energyMeters.size(); i++) {
EnergyReading reading = new EnergyReading();
reading.setMeter(energyMeters.get(i));
//the two lines below done temporary so I don't have to fill the form every time
// I test it now
reading.setReading(23);
reading.setDate(new Date());
readings.add(reading);
}
energyReadingWrapper.setEnergyReadings(readings);
Map<String, Object> model = new HashMap<String, Object>();
model.put("energyReadingWrapper", energyReadingWrapper);
return new ModelAndView("RegisterEnergyReadings", "model", model);
}
@RequestMapping("/saveEnergyReadings")
public ModelAndView saveEnergyReadings( @ModelAttribute("energyReadingWrapper") EnergyReadingWrapper energyReadingWrapper, BindingResult result) {
System.out.println("this is the point I reach before the null exception");
System.out.println(energyReadingWrapper.toString());
List<EnergyReadings> readings= energyReadingWrapper.getEnergyReadings();
for (EnergyReadingsi : readings) {
readingService.saveReading(i);
}
return new ModelAndView("redirect:/readingList.html");
}
和jsp的形式(它的关键部分):
<c:url var="readings" value="saveEnergyReadings.html" />
<form:form id="energyReadingWrapper"
modelAttribute="energyReadingWrapper" method="post"
action="${readings}">
<table width="400px" height="150px">
<c:forEach items="${energyReadingWrapper.energyReadings}"
varStatus="i" var="energyReadings">
<tr>
<td><c:out value="${energyReadings.meter.description}" /></td>
<td><form:input
path="energyReadings[${i.index}].readingValue" /></td>
<td><form:input path="energyReadings[${i.index}].date"
type="date" /></td>
</tr>
</c:forEach>
<tr>
<td colspan="6" align="center"><input type="submit"
value="Save" /></td>
</table>
</form:form>
一切都很美好。 jsp页面生成的行具有与计数器相匹配的行,并且它们都具有我设置的默认值(23和当前日期)我不明白为什么当按下保存按钮时包装器未传递到控制器。 我错过了什么?
问候,Maciej
编辑:堆栈跟踪(不幸的是我正在用波兰语写程序):
键入例外报告
消息请求处理失败; 嵌套异常是java.lang.NullPointerException
说明服务器遇到一个内部错误,导致它无法完成此请求。
例外
org.springframework.web.util.NestedServletException:请求处理失败; 嵌套异常是java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)javax.servlet.http。 HttpServlet.service(HttpServlet.java:648)javax.servlet.http.HttpServlet.service(HttpServlet.java:729)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)根本原因
java.lang.NullPointerException forms.OdczytEnergiaFormularz.toString(OdczytEnergiaFormularz.java:20)kamienica.controller.HomeController.zapiszOdczytEnergia(HomeController.java:511)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(未知源代码)sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)java.lang.reflect.Method.invoke(Unknown Source)org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)org .springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)org.springframework.web.servlet .mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:77 1)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)org.springframework.web.servlet.FrameworkServlet.doPost( FrameworkServlet.java:563)javax.servlet.http.HttpServlet.service(HttpServlet.java:648)javax.servlet.http.HttpServlet.service(HttpServlet.java:729)org.apache.tomcat.websocket.server.WsFilter。的doFilter(WsFilter.java:52)
这是包装类:
public class EnergyReadingWrapper{
private List<EnergyReading> energyReadings;
(setters and getters)
}
链接地址: http://www.djcxy.com/p/27887.html