【问题标题】:setting an entity property using a form and controller使用表单和控制器设置实体属性
【发布时间】:2014-02-28 22:55:20
【问题描述】:

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

谁能告诉我如何让这个功能正常工作而不会引发错误?

当我将以下代码行添加到我的 JSP 时,我可以创建 400 error

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

这是处理此 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}";
    }
}

供参考,处理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 providerId 在 url 字符串中。并且可以通过从 JSP 中删除行 &lt;form:select path="location" items="${praddrs}" size="3" style="min-width:200px"/&gt; 来消除该错误。如果您仍然认为@RequestParam 是问题所在,您能否展示如何修复代码来解决问题?
  • 如果您删除表单,错误就会消失?所以您没有通过表单提交访问 servlet?那我完全被你的问题弄糊涂了。
  • @developerwjk 如果我删除指定的代码行 &lt;form:select path="location" items="${praddrs}" size="3" style="min-width:200px"/&gt; ,则允许控制器 POST 方法运行,并且在方法结束时,出现与没有值相关的错误数据库中映射位置的列。 JSP 中给定的代码行触发 400 错误。我的问题是如何找到更好的方式让用户选择他们相遇的地点。

标签: java spring hibernate jsp spring-mvc


【解决方案1】:

把“location”改成“location.id”怎么样?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多