在一個Spring MVC應用程序中使用的休眠的實體屬性,我當用戶試圖指定(實體是FacilityAddress
)的location
一個appointment
(實體是Encounter
)使用JSP遇到錯誤。具體而言,需要由JSP設置屬性Encounter.location
。可能的位置列表通過模型屬性praddrs
提供給用戶,該屬性通過控制器中的GET方法正確填充到JSP表單中。設定使用形式和控制器
有人可以告訴我如何讓這個功能正常工作,而不會引發錯誤嗎?
我可以創建400 error
當我下面的代碼行添加到我的JSP:
<form:select path="location" items="${praddrs}" size="3" style="min-width:200px"/>
的400 error
狀態:
The request sent by the client was syntactically incorrect.
有一個在Eclipse控制檯沒有堆棧跟蹤。
我已經閱讀了一些其他的堆棧溢出發貼,並已完成谷歌搜索與修補我的代碼,但我似乎無法隔離我的獨特情況的解決方案。有人能告訴我如何克服這個錯誤嗎?
這裏是在JSP形式:
<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>
這裏是用於處理POST
這個JSP控制器方法:
//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}";
}
}
作爲參考,用於處理控制器方法JSP的GET
是:
@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";
}
我已發佈了將實體代碼添加到文件共享網站,以便可以更輕鬆地閱讀此郵件。您可以通過點擊以下鏈接閱讀實體代碼:
Encounter
實體代碼可以被讀取at this link。
FacilityAddress
實體代碼可以被讀取at this link。
BaseEntity代碼可以被讀取爲at this link。
很可能是因爲您將providerId定義爲@RequestParam,但沒有將它放在表單中。 – developerwjk
@developerwjk'providerId'在url字符串中。這個錯誤可以通過從JSP中刪除'
''來刪除。如果你仍然認爲'@ RequestParam'是問題,你能展示如何修復代碼來解決問題嗎? – CodeMed如果刪除表單,錯誤消失?所以你不通過表單提交訪問servlet?然後,我完全被你的問題弄糊塗了。 – developerwjk