小编Mir*_*scu的帖子

更新形式:Spring MVC +百里香叶

我正在尝试创建一个百里香形式来仅更新支持对象的几个属性:

@RequestMapping(value = "/jobs/{id}", method = RequestMethod.GET)
public ModelAndView update(@PathVariable Integer id ) {
    ModelAndView mav = new ModelAndView("updateJob.html");
    JobDescription updateJob = jobDescriptionService.findByID(id);
    mav.addObject("updateJob", updateJob);
    return mav;
}

@RequestMapping(value = "/jobs/{id}", method = RequestMethod.PUT)
public String saveUpdate(@PathVariable Integer id, @ModelAttribute("updateJob") JobDescription updateJob) {
    jobDescriptionService.update(updateJob);
    return "redirect:/jobs/" + id;
}


<form th:action="@{'/jobs/'+ ${updateJob.id}}" th:object="${updateJob}" th:method="PUT">
    <table>
        <tr>
            <td><label>Description</label></td>
            <td><input type="text" th:field="*{description}" /></td>
        </tr>
        <tr>
            <td><label>Deadline</label></td>
            <td><input type="text" th:field="*{deadline}" /></td>
        </tr>
        <tr>
            <td></td>
            <td><button type="submit">Update</button></td>
        </tr>
    </table>
</form>
Run Code Online (Sandbox Code Playgroud)

问题是作业对象还有一些其他我不想更新的属性(例如id,createdDate等)。但是,当我单击更新表单的提交按钮时,在saveUpdate方法中创建的对象会将这些属性设置为null(除非我在表单内部的隐藏字段中设置了这些属性)。我还有其他办法可以保留它们吗?

spring-mvc thymeleaf

5
推荐指数
1
解决办法
8384
查看次数

标签 统计

spring-mvc ×1

thymeleaf ×1