属性未从视图发送到控制器

时间:2014-02-26 03:28:10

标签: java spring hibernate jsp

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

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

这是JSP:

CPTCode

这是控制器方法:

<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] 实体的代码at this link 可以阅读Encounter实体的代码at this link 可以找到CPTCode类的代码at this link.
Patient的代码为at this link.
Person的代码为at this link.

注意:

删除行BaseEntity可以消除错误消息,但也会删除下拉列表,这是此JSP的核心。如何让下拉列表起作用?

3 个答案:

答案 0 :(得分:1)

从您的stacktrace消息:

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

这意味着id的{​​{1}}属性为null,当您使用它时,该时间将引发CPTCode

因此,要使代码正常工作,请更改以下内容:

要在表单中添加带有CPTCode的选择框,请修改如下:

NullPointerException

然后,使用getter和setter将变量 <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

并在你的POST修改如下:

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

答案 1 :(得分:0)

Spring MVC正试图从你的CPTCode对象中获取值

CPTCode.getId(CPTCode.java:30)

但是因为你传入了一个emtpy对象,所以它们是空的

CPTCode code = new CPTCode();
model.put("code", code);

将一些值填充到code中,让您的类在默认构造函数中初始化值,或让您的类在getter中返回默认值,这样您就不会返回null。

我建议您考虑熟悉Null Object Pattern.

答案 2 :(得分:0)

删除下拉菜单并尝试

<form:form modelAttribute="code" method="${method}" class="form-horizontal">

     <input type='text' name="id" name='id' value='100'>




   <div class="form-actions">
   <button type="submit">Add a Billing Code</button> <h3>    Link to delete will go here.</h3>
   </div>
  </form:form>

尝试这个,你应该工作..所以你也可以按照这个列表:)