【问题标题】:Edit form validation with Spring MVC使用 Spring MVC 编辑表单验证
【发布时间】:2013-04-15 18:44:41
【问题描述】:

我在我的项目中使用 Spring MVC 和 Spring 表单验证。 对象模型中有一个名为Group 的类,我创建了用于编辑它的表单。

表格

<spring:url var="saveGroup" value="/teacher/groups/save"/>
<form:form action="${saveGroup}" method="post" modelAttribute="group">

    <form:hidden path="id"/>

    <div id="nameDiv" class="control-group">
        <form:label path="title">Title:</form:label>
        <form:input path="title"/>
        <form:errors path="title"/>
    </div>

    <div id="specDiv" class="control-group">
        <form:label path="title">Specialty:</form:label>
        <form:select path="specialty">
            <form:options items="${specialties}" itemValue="id" itemLabel="title"/>
        </form:select>
    </div>

    <div class="center">
        <spring:url var="groups" value="/teacher/groups"/>
        <input class="btn btn-primary" type="submit" value="Save"/>
        <a class="btn" href="${groups}"> Cancel </a>
    </div>
</form:form>

控制器

@Controller
@RequestMapping("/teacher/groups")
public class GroupsController {

    @Autowired
    private GroupService groupService;
    @Autowired
    private SpecialtyService specialtyService;

    @ModelAttribute("group")
    public Group setGroup(Long id) {
        if (id != null) {
            return groupService.read(id);
        } else {
            return new Group();
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Specialty.class, "specialty", 
            new SpecialtyEditor(specialtyService));
        binder.setValidator(new GroupValidator());
    }

    @RequestMapping("")
    public ModelAndView groups() {
        return new ModelAndView("teacher/groups/list", "groups", 
            groupService.list());
    }

    @RequestMapping("/edit")
    public ModelAndView editGroup() {
        return new ModelAndView("teacher/groups/edit", "specialties",
            specialtyService.list());
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveGroup(@Valid Group group, BindingResult result) {
        if (result.hasErrors()) {
            return "forward:/teacher/groups/edit";
        }
        groupService.update(group);
        return "redirect:/teacher/groups";
    }
}

我想在验证失败的情况下设置表单的适当行为。 IE。它应该保存其状态,但只显示验证错误消息(如使用 javascript 验证时)。 我认为“转发:/teacher/groups/edit”将再次将请求转发到editGroup(),并保存对象groupresult。但是,当我验证表单失败时,只需重新加载并显示已编辑 group 的开始状态:没有错误,也没有保存的更改。 我怎样才能正确地做到这一点?

谢谢!

【问题讨论】:

    标签: java forms validation spring-mvc


    【解决方案1】:

    我通过不将请求转发给其他方法而是立即向用户发送答案来解决问题。现在它可以工作了,看起来像:

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveGroup(@Valid Group group, BindingResult result) {
        if (result.hasErrors()) {
            return new ModelAndView("/teacher/groups/edit", "specialties", specialtyService.list());
        }
        groupService.update(group);
        return new ModelAndView("redirect:/teacher/groups");
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多