在使用hibernate模式的spring mvc应用程序中,当用户从表单的下拉列表中选择了值之后,单击用户提交按钮时,JSP不会传递名为codetype 的对象的填充值。结果,当运行jsp的控制器方法时,出现了空指针异常。 有人可以告诉我如何修复我的代码,以使空指针错误消失并且控制器可以看到 用户选择的代码吗?CPTCode``code code
code
CPTCode``code
的code是从可能的码列表的预设选择,并以基准code然后被添加到的ArrayList属性Encounter,其具有带有多对多关系的实体CPTCode。
Encounter
CPTCode
这是JSP:
<html lang="en"> <jsp:include page="../fragments/headTag.jsp"/> <body> <div class="container"> <jsp:include page="../fragments/bodyHeader.jsp"/> <c:set var="method" value="put"/> <h2>Codes</h2> <form:form modelAttribute="code" method="${method}" class="form-horizontal"> <div class="control-group" id="patient"> <label class="control-label">Patient </label> <c:out value="${encounter.patient.firstName} ${encounter.patient.lastName}"/> ${encounter.dateTime} </div> <div class="control-group"> <form:select path="${code}" items="${encountercodes}" size="5" style="min-width:600px"/> </div> <td></td> <div class="form-actions"> <button type="submit">Add a Billing Code</button> <h3> Link to delete will go here.</h3> </div> </form:form> </div> </body> </html>
这是控制器方法:
@RequestMapping(value = "/patients/{patientId}/encounters/{encounterId}/codes", method = RequestMethod.GET) public String initUpdateCodesForm(@PathVariable("encounterId") int encounterId, Map<String, Object> model) { System.out.println("--------------------------------- made it into initUpdateForm() method"); Encounter encounter = this.clinicService.findEncounterById(encounterId); CPTCode code = new CPTCode(); model.put("code", code); model.put("encounter", encounter); return "encounters/createOrUpdateCodesForm"; } @RequestMapping(value = "/patients/{patientId}/encounters/{encounterId}/codes", method = {RequestMethod.PUT, RequestMethod.POST}) public String processUpdateCodesForm(@ModelAttribute("code") CPTCode code, @PathVariable("encounterId") int eid, BindingResult result, SessionStatus status) { Encounter encounter = this.clinicService.findEncounterById(eid); System.out.println("-------- code.id and code.name are: "+code.getId()+", "+code.getName());//null error here int maxId = 0; for(int u=0;u<encounter.getCodes().size();u++){ if(encounter.getCodes().get(u).getId()>maxId){ maxId = encounter.getCodes().get(u).getId(); } } code.setId(maxId+1); encounter.addCode(code); System.out.println("... in processUpdateCodesForm() just did encounter.addCode(code)"); this.clinicService.saveEncounter(encounter); System.out.println("..... encounter.id, encounter.codes.size are: "+encounter.getId()+", "+encounter.getCodes().size()); return "redirect:/encounters?encounterID={encounterId}"; }
这是完整的堆栈跟踪:
java.lang.NullPointerException: null at org.springframework.samples.knowledgemanager.model.CPTCode.getId(CPTCode.java:30) ~[CPTCode.class:na] at org.springframework.samples.knowledgemanager.web.EncounterCodeController.processUpdateCodesForm(EncounterCodeController.java:104) ~[EncounterCodeController.class:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29] at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29] at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:849) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) [servlet-api.jar:na] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:74) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42] at com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:73) [datatables-core-0.9.2.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42] at com.github.dandelion.datatables.extras.servlet2.filter.DatatablesFilter.doFilter(DatatablesFilter.java:71) [datatables-servlet2-0.9.2.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.42] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.42] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:7.0.42] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.42] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.42] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [catalina.jar:7.0.42] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.42] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.42] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) [tomcat-coyote.jar:7.0.42] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.42] at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-coyote.jar:7.0.42] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_29] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_29] at java.lang.Thread.run(Thread.java:662) [na:1.6.0_29]
可以通过以下链接在文件共享站点上读取实体的代码:
Encounter可以在此链接中读取实体的代码。可以在此链接中读取实体 的代码。该类 的代码可以在此链接中找到。 对于该代码是在这个环节。 对于该代码是在这个环节。CPTCode Patient Person BaseEntity
Patient
Person
BaseEntity
注意:
删除该行不仅可以<form:select path="${code}" items="${encountercodes}" size="5" style="min-width:600px"/>消除错误消息,而且还可以删除下拉列表,该列表是此JSP的核心。如何使下拉列表起作用?
<form:select path="${code}" items="${encountercodes}" size="5" style="min-width:600px"/>
从您的stacktrace消息中:
java.lang.NullPointerException: null at org.springframework.samples.knowledgemanager.model.CPTCode.getId(CPTCode.java:30) ~[CPTCode.class:na]
这表示的id属性CPTCode为null,当您使用它时,该时间将引发一个NullPointerException。
id
NullPointerException
因此,要使用您的代码,请更改以下内容:
要在表单中添加带有CPTCode的选择框,请进行如下修改:
<form:form modelAttribute="encounter" method="post" class="form-horizontal" action="${actUrl}"> <div class="control-group"> <form:select path="codeSelected" items="${encountercodes}" size="5" style="min-width:600px"/> </div> <form:hidden path="id"/> <td> </td> <div class="form-actions"> <button type="submit">Add a Billing Code</button> <h3> Link to delete will go here.</h3> </div> </form:form>
然后,使用getter和setter 将变量添加private Integer codeSelected;到您的Encounter类中。
private Integer codeSelected;
在您的控制器中填充遭遇代码,例如:
@ModelAttribute("encountercodes") public Map populateEncountercodes() { Map<Integer, String> encCodes = new LinkedHashMap<Integer, String>(); for(CPTCode cpt: this.clinicService.findEncountercodes()){ encCodes.put(cpt.getId(), cpt.getName()); } return encCodes; }
并在您的POST中进行如下修改:
@RequestMapping(value = "/patients/{patientId}/encounters/{encounterId}/codes", method = {RequestMethod.POST}) public String processUpdateCodesForm(@ModelAttribute("encounter") Encounter encounter, @PathVariable("encounterId") int eid, BindingResult result, SessionStatus status) { Encounter myencounter = this.clinicService.findEncounterById(eid); CPTCode myCode = this.clinicService.findCPTCodeById(encounter.getCodeSelected()); myencounter.addCode(myCode); return "redirect:/encounters?encounterID={encounterId}"; }