在使用hibernate和jpa的spring mvc应用程序中,我无法让实体从get
方法转到post
方法,这会导致其他下游问题。有人可以告诉我如何进行设置,以便在post
方法中仍然可以看到实体的属性吗?
以下是控制器方法:
@RequestMapping(value = "/patients/{patientId}/encounters/{encounterId}/providers", method = RequestMethod.GET)
public String initUpdateProvidersForm(@PathVariable("encounterId") int encounterId, Model model) {
Encounter encounter = this.clinicService.findEncounterById(encounterId);
ArrayList<Provider> myproviders = new ArrayList<Provider>();
myproviders.addAll(encounter.getProviders());
System.out.println("............encounter id is: "+encounter.getId());
model.addAttribute("encounter", encounter);
return "encounters/addProvidersForm";
}
@RequestMapping(value = "/patients/{patientId}/encounters/{encounterId}/providers", method = {RequestMethod.POST})
public String processUpdateProvidersForm(@ModelAttribute("encounter") Encounter encounter, @PathVariable("encounterId") int eid, BindingResult result, SessionStatus status) {
Provider provider = this.clinicService.findProviderById(encounter.getProviderSelected());
encounter.addProvider(provider);
System.out.println(" :::::: encounter id is: "+encounter.getId());
this.clinicService.saveEncounter(encounter);
return "redirect:/encounters?encounterID={encounterId}";
}
注意: encounter.getId()
在GET
打印出一个数字,但在null
中打印出POST
。为什么?我怎样才能解决这个问题?
这是JSP:
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %>
<html lang="en">
<jsp:include page="../fragments/headTag.jsp"/>
<body>
<div class="container">
<jsp:include page="../fragments/bodyHeader.jsp"/>
<c:set var="method" value="post"/>
<h2>Providers</h2>
<div class="control-group" id="patient">
<label class="control-label">Patient </label>
<c:out value="${encounter.patient.firstName} ${encounter.patient.lastName}"/>
${encounter.dateTime}
</div>
<c:url value="/patientscodes.html" var="actUrl"/>
${encounter.id}
<form:form modelAttribute="encounter" method="${method}" class="form-horizontal" >
<div class="control-group">
<form:select path="providerSelected" items="${providers}" size="5" style="min-width:600px"/>
</div>
<form:hidden path="id"/>
<td>
</td>
<div class="form-actions">
<button type="submit">Add a Provider</button> <h3> Link to delete will go here.</h3>
</div>
</form:form>
</div>
</body>
</html>