更新表格:Spring mvc + thymeleaf

时间:2014-11-27 09:56:14

标签: spring-mvc thymeleaf

我试图创建一个百日咳形式来更新后备对象的几个属性:

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

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

4 个答案:

答案 0 :(得分:2)

我和你有同样的问题,所以我已经建立了自己的解决方案

1-您需要在控制器上执行两项操作:查看(GET)和操作(POST)

@GetMapping("/user/edit/{userId}")
public ModelAndView editUserView(@PathVariable Long userId) throws NotFoundException {

    User user = this.userService.load(userId);

    if (user == null) {
        throw new NotFoundException("Not found user with ID " + userId);
    }

    ModelAndView modelAndView = new ModelAndView();

    modelAndView.setViewName("user.edit");
    modelAndView.addObject("user", user);

    return modelAndView;
}

@PostMapping("/user/edit/{userId}")
public ModelAndView editUserAction(HttpServletRequest request, @PathVariable Long userId, @Validated(User.ValidationUpdate.class) User userView,
        BindingResult bindingResult) throws Exception {

    User user = this.userService.load(userId);

    if (user == null) {
        throw new NotFoundException("Not found user with ID " + userId);
    }

    ModelAndView modelAndView = new ModelAndView();
    if (bindingResult.hasErrors()) {            
        modelAndView.setViewName("user.edit");
        modelAndView.addObject("user", userView);

        return modelAndView;
    }

    Form.bind(request, userView, user);

    this.userService.update(user);

    modelAndView.setViewName("redirect:/admin/user");

    return modelAndView;
}

2-一个显示错误的视图(非常重要:添加隐藏的输入以发送id以进行验证)

<fieldset th:if="${#fields.hasErrors('${user.*}')}" class="text-warning">
    <legend>Some errors appeared !</legend>
    <ul>
        <li th:each="err : ${#fields.errors('user.*')}" th:text="${err}"></li>
    </ul>
</fieldset>

 <form action="#" th:action="@{/admin/user/edit/{id}(id=${user.id})}" th:object="${user}" method="post">
      <div th:class="${#fields.hasErrors('firstName')} ? 'form-group has-error' : 'form-group'">
          <label class="control-label" for="firstName">First Name <span class="required">*</span></label>
          <input type="text" th:field="*{firstName}" required="required">
      </div>
      ...
    <input type="hidden" th:field="*{id}">
</form>

3-对于我的例子,我写了一个FormUtility类来合并两个对象:

public static List<String> bind(HttpServletRequest request, Object viewObject, Object daoObject) throws Exception {

    if (viewObject.getClass() != daoObject.getClass()) {
        throw new Exception("View object and dao object must have same type (class) !");
    }

    List<String> errorsField = new ArrayList<String>();

    // set field value
    for (Entry<String, String[]> parameter : request.getParameterMap().entrySet()) {

        // build setter/getter method
        String setMethodName = "set" + parameter.getKey().substring(0, 1).toUpperCase()
                + parameter.getKey().substring(1);
        String getMethodName = "get" + parameter.getKey().substring(0, 1).toUpperCase()
                + parameter.getKey().substring(1);

        try {
            Method getMethod = daoObject.getClass().getMethod(getMethodName);
            Method setMethod = daoObject.getClass().getMethod(setMethodName, getMethod.getReturnType());
            setMethod.invoke(daoObject, getMethod.invoke(viewObject));
        }
        catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException exception) {
            errorsField.add(parameter.getKey());
        }
    }

    return errorsField;
}

希望这对你有所帮助。

答案 1 :(得分:1)

最安全的方法是通过id加载原始作业对象,并在其上设置新值,然后更新它......如下所示:

JobDescription originalJob = jobDescriptionService.findById(updateJob.getId());

originalJob.setParamForUpdate(updateJob.getParamForUpdate());
originalJob.setAnotherParamForUpdate(updateJob.getAnotherParamForUpdate());

jobDescriptionService.update(originalJob);

这将保存您想要保持不变的所有数据......

答案 2 :(得分:0)

{{1}}

答案 3 :(得分:-4)

var editOptions = {
    url: '/Admin/EditPost', 
    editCaption: 'Edit Post',
    processData: "Saving...",
    width: 900,
    closeAfterEdit: true,
    closeOnEscape: true,
    afterclickPgButtons: afterclickPgButtons,
    afterShowForm: afterShowForm,           
    onClose: onClose,
    afterSubmit: JustBlog.GridManager.afterSubmitHandler,
    beforeSubmit: beforeSubmitHandler
};

$(gridName).navGrid(pagerName, 
{
    cloneToTop: true,
    search: false
}, 
editOptions, addOptions, {});