使用表单和控制器设置实体属性

时间:2014-02-28 22:55:20

标签: java spring hibernate jsp spring-mvc

在使用hibernate的Spring MVC应用程序中,当用户尝试指定location的{​​{1}}(实体为FacilityAddress)时,我遇到错误(实体为{{1使用JSP。具体来说,属性appointment需要由JSP设置。可能的位置列表由模型属性Encounter提供给用户,该属性通过控制器中的GET方法在JSP表单中正确填充。

有人可以告诉我如何在不抛出错误的情况下使此功能正常工作吗?

当我向JSP添加以下代码行时,我可以创建Encounter.location

praddrs

400 error州:

<form:select path="location" items="${praddrs}" size="3" style="min-width:200px"/>  

eclipse控制台中没有堆栈跟踪。

我已经阅读了其他一些堆栈溢出帖子,并且已经完成谷歌搜索以及修补我的代码,但我似乎无法为我的独特情况孤立解决方案。有人可以告诉我如何通过这个错误?

以下是JSP中的表单:

400 error

以下是处理此JSP的The request sent by the client was syntactically incorrect. 的控制器方法:

<form:form modelAttribute="encounter" 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}"/>
    </div>
    <div class="control-group" id="datetime">
        <label class="control-label">Date and Time </label>
        <joda:format value="${encounter.dateTime}" style="SM"/>
    </div>

    <div class="control-group">
        <label class="control-label">Office</label>
        <form:select path="location" items="${praddrs}" size="3" style="min-width:200px"/>
    </div>

    <petclinic:inputField label="Duration (mins)" name="numMins"/>

    <td>
    </td>
    <div class="form-actions">
        <c:choose>
            <c:when test="${encounter['new']}">
                <button type="submit">Add Encounter</button>
            </c:when>
            <c:otherwise>
                <button type="submit">Update Encounter</button> <h3>    Link to delete will go here.</h3>
            </c:otherwise>
        </c:choose>
    </div>
</form:form>

作为参考,处理JSP POST的控制器方法是:

//THIS IS THE METHOD THAT HANDLES THE FORM
@RequestMapping(value = "/patients/{patientId}/encounters/new", method = RequestMethod.POST)
public String processCreationForm(@ModelAttribute("encounter") Encounter encounter, @RequestParam("providerId") int providerId, BindingResult result, SessionStatus status) {
    System.out.println("inside processCreationForm() ");
    System.out.println("encounter.getDateTime() is: "+encounter.getDateTime());
    new EncounterValidator().validate(encounter, result);
    Provider myprovider = this.clinicService.findProviderById(providerId);
    encounter.addProvider(myprovider);
    if (result.hasErrors()) {
        System.out.println("about to return errors. ");
        return "encounters/createOrUpdateEncounterForm";
    } else {
        System.out.println("about to save encounter ");
        this.clinicService.saveEncounter(encounter);
        System.out.println("done saving encounter.");
        status.setComplete();
        System.out.println("finished status.setComplete()");
        return "redirect:/encounters?encounterID={encounterId}";
    }
}

我已将实体代码发布到文件共享网站,因此这篇文章可以更容易阅读。您可以通过单击以下链接来阅读实体代码:

可以阅读GET实体代码at this link 可以阅读@RequestMapping(value = "/patients/{patientId}/encounters/new", method = RequestMethod.GET) public String initCreationForm(@PathVariable("patientId") int patientId, @RequestParam("providerId") int prid, org.springframework.web.context.request.WebRequest webRequest, Map<String, Object> model) { Patient patient = this.clinicService.findPatientById(patientId); LocalDate theday = new LocalDate(webRequest.getParameter("day")); LocalTime thetime = new LocalTime(webRequest.getParameter("time")); DateTime thedatetime = theday.toDateTime(thetime); this.clinicService.findFacilityAddressByProviderId(prid); Encounter encounter = new Encounter(); encounter.setDateTime(thedatetime); patient.addEncounter(encounter); ArrayList<FacilityAddress> praddrs = (ArrayList<FacilityAddress>) this.clinicService.findFacilityAddressByProviderId(prid); Provider pr = clinicService.findProviderById(prid); model.put("encounter", encounter); model.put("praddrs", praddrs); model.put("pr", pr); return "encounters/createOrUpdateEncounterForm"; } 实体代码at this link BaseEntity代码可以读取at this link

1 个答案:

答案 0 :(得分:0)

如何将“location”更改为“location.id”?

<form:select path="location.id" items="${praddrs}" size="3" style="min-width:200px"/>