属性没有从视图发送到控制器

属性没有从视图发送到控制器

本文介绍了属性没有从视图发送到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用hibernate的spring mvc应用程序中,JSP没有为类型的类型的< CPTCode 对象传递填充值$ c>当用户从表单的下拉列表中为 code 选择一个值后点击提交按钮。因此,当jsp的控制器方法运行时,我得到一个空指针异常。 有人能告诉我如何修复我的代码,以便空指针错误消失,并且控制器可以看到用户选择的代码?



代码是从预设的可能代码列表中选择的,并且对 code 然后被添加到 Encounter 实体的arraylist属性,该实体与 CPTCode

以下是JSP:

 < html lang =en> 
< jsp:include page =../ fragments / headTag.jsp/>
< body>
< div class =container>
< jsp:include page =../ fragments / bodyHeader.jsp/>
< c:set var =methodvalue =put/>
< h2>代码< / h2>
< form:form modelAttribute =codemethod =$ {method}class =form-horizo​​ntal>
< div class =control-groupid =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 =5style =min-width:600px/>
< / div>
< td>< / td>
< div class =form-actions>
< button type =submit>添加帐单代码< / button> < H3>链接删除将会在这里。< / h3>
< / div>
< / form:form>
< / div>
< / body>
< / html>

以下是控制器方法:

<$ p
public String initUpdateCodesForm(@PathVariable(encounterId); $ p $ @RequestMapping(value =/ patients / {patientId} / encounters / {encounterId} / codes,method = RequestMethod.GET) )int encounterId,Map< String,Object> model){
System.out.println(------------------------- --------使它成为initUpdateForm()方法);
遭遇遇到= this.clinicService.findEncounterById(encounterId);
CPTCode code = new CPTCode();
model.put(code,code);
model.put(遇到,遇到);
返回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结果,SessionStatus状态){
Encounter encounter = this.clinicService.findEncounterById(eid);
System.out.println(-------- code.id和code.name是:+ code.getId()+,+ code.getName()); // null错误在这里
int maxId = 0; (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是:+ encounter.getId()+,+ encounter.getCodes()。size());
返回重定向:/遇到?遇到ID = {遇难ID};
}

以下是完整的堆栈跟踪:

  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。调用(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.springfram ework.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。 (AbstractHandlerMethodAdapter.java:80)〜[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(Disp atletServlet.java:925)[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)[spring -webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)[spring-webmvc-3.2.5.RELEASE .jar:3.2.5.RELEASE]
在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]
在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)[servlet-api.jar :na]
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)[catalina.jar:7.0.42]
在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]
在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)[spring-web-3.2.5.RELEASE.jar :3.2.5.RELEASE]
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)[catalina.jar:7.0.42]
在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]
在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(ApplicationFi lterChain.java:210)[catalina.jar:7.0.42]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)[catalina.jar:7.0.42]
在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]
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)[catalina.jar:7.0.42]
在org.apache.catalina。 connector.CoyoteAdapter.service(CoyoteAdapter.java:408)[catalina.jar:7.0.42]
at org.apache.coyote.http11.Abstrac tHttp11Processor.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]
在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 的代码是



注意:



删除行< 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]

它表示 CPTCode 的 id / code>为空,当你使用它时,那个时候会产生一个 NullPointerException 。



,为了让你的代码工作,请更改以下内容:



要在表单中添加CPTCode的选择框,请修改为:

 < form:form modelAttribute =encountermethod =postclass =form-horizo​​ntalaction =$ {actUrl}> 
< div class =control-group>
< form:select path =codeSelecteditems =$ {encountercodes}size =5style =min-width:600px/>
< / div>
< form:hidden path =id/>
< td>
< / td>
< div class =form-actions>
< button type =submit>添加帐单代码< / button> < H3>链接删除将会在这里。< / h3>
< / div>
< / form:form>

然后,添加一个变量 private Integer codeSelected; 添加到 Encounter 类中,使用getter和setter。



在您的控制器中填充邂逅代码:

  @ModelAttribute(encountercodes)
public Map populateEncountercodes(){
Map< Integer,String> encCodes = new LinkedHashMap< Integer,String>(); (CPTCode cpt:this.clinicService.findEncountercodes()){
encCodes.put(cpt.getId(),cpt.getName());
}
返回encCodes;
}

并且在您的POST中修改如下:

  @RequestMapping(value =/ patients / {patientId} / encounters / {encounterId} / codes,method = {RequestMethod.POST})
public字符串processUpdateCodesForm(@ModelAttribute(遇到)遇到遇到,
@PathVariable(encounterId)int eid,BindingResult结果,SessionStatus状态){
Encounter myencounter = this.clinicService.findEncounterById(eid);
CPTCode myCode = this.clinicService.findCPTCodeById(encounter.getCodeSelected());
myencounter.addCode(myCode);
返回重定向:/遇到?遇到ID = {遇难ID};
}


In a spring mvc application using hibernate, a JSP is not passing a populated value for an object called code of type CPTCode when the user clicks the submit button after selecting a value for code from the drop down list in the form. As a result, I am getting a null pointer exception when the controller method for the jsp runs. Can someone show me how to fix my code so that the null pointer error goes away and the controller can see the code which the user selected?

The code is selected from a preset of list of possible codes, and a reference to the code is then added to an arraylist property of an Encounter entity which has a ManyToMany relationship with CPTCode.

Here is the 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>

Here is the controller method:

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

Here is the complete stack trace:

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]

The code for the entities can be read at a file sharing site by clicking on the links below:

The code for the Encounter entity can be read at this link.
The code for the CPTCode entity can be read at this link.
The code for the Patient class can be found at this link.
The code for Person is at this link.
The code for BaseEntity is at this link.

NOTE:

Deleting the line <form:select path="${code}" items="${encountercodes}" size="5" style="min-width:600px"/> eliminates the error message, but also deletes the drop down list, which is central to this JSP. How can I get the drop down list to work?

解决方案

From your stacktrace message :

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

it means the id attribute of CPTCode is null, when you use it, that time will raise a NullPointerException.

So, To get work your code, change the following:

To add a select box with CPTCode in your form, modify like:

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

then, add a variable private Integer codeSelected; to your Encounter class, with getter and setter.

Populate encountercodes in your controller like:

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

And In your POST modify like:

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

这篇关于属性没有从视图发送到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:18