gpt4 book ai didi

java - 属性未从 View 发送到 Controller

转载 作者:行者123 更新时间:2023-12-01 13:20:58 27 4
gpt4 key购买 nike

在使用 hibernate 的 spring mvc 应用程序中,JSP 未传递名为 code 的对象的填充值。类型 CPTCode当用户在选择 code 的值后单击提交按钮时从表单的下拉列表中。结果,当 jsp 的 Controller 方法运行时,我收到空指针异常。 有人可以告诉我如何修复我的代码,以便空指针错误消失并且 Controller 可以看到 code用户选择了哪个?

code从预设的可能代码列表中选择,并引用 code然后添加到 Encounter 的 arraylist 属性中与CPTCode有ManyToMany关系的实体.

这是 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>

这是 Controller 方法:

@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实体可以读取 at this link .
代码为CPTCode实体可以读取 at this link .
代码为Patient可以找到类at this link.
代码 Personat this link.
代码 BaseEntityat this link.

注意:

删除行<form:select path="${code}" items="${encountercodes}" size="5" style="min-width:600px"/>消除了错误消息,同时也删除了该 JSP 的核心下拉列表。如何使下拉列表起作用?

最佳答案

来自您的堆栈跟踪消息:

java.lang.NullPointerException: null
at org.springframework.samples.knowledgemanager.model.CPTCode.getId(CPTCode.java:30) ~[CPTCode.class:na]

这意味着CPTCodeid属性为null,当您使用它时,会引发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>

然后,将变量 private Integer codeSelected; 添加到您的 Encounter 类,并使用 getter 和 setter。

在 Controller 中填充遭遇代码,例如:

@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}";
}

关于java - 属性未从 View 发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031119/

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