【问题标题】:Spring MVC - Not getting object in view after validation error [duplicate]Spring MVC - 验证错误后未查看对象[重复]
【发布时间】:2016-06-02 06:44:29
【问题描述】:

我正在尝试使用 Spring MVC 验证表单,并且它正在工作,但是当包含错误消息的视图出现时,我使用从控制器发送的对象通过 forEach 创建的字段没有被创建。

form.jsp

<form:form action="/project/product" method="POST" commandName="product">
    <div>
        <label>Title</label>
        <input type="text" name="title">
        <form:errors path="title"></form:errors>
    </div>
    <div>
        <label>Description</label>
        <textarea rows="10" cols="20" name="Description"></textarea>
        <form:errors path="Description"></form:errors>
    </div>
    <c:forEach items="${types}" var="priceType" varStatus="status">
        <div>
            <label>${priceType.nome}</label>
            <input type="text" name="prices[${status.index}].value"> 
            <input type="hidden" name="prices[${status.index}].type" value=${priceType }>   
        </div>
    </c:forEach>

    <button type="submit">Save</button>
</form:form>

控制器:

@RequestMapping("/form")
public ModelAndView form(ModelMap m) {
    m.addAttribute("product", new Product());
    ModelAndView modelAndView = new ModelAndView("products/form");
    modelAndView.addObject("types",PriceType.values());

    return modelAndView;
}

@RequestMapping(method=RequestMethod.POST)
public String grava(@Valid Product product, BindingResult result, RedirectAttributes redirectAtributes, ModelMap m) {
    if(result.hasErrors()) {
        return "products/form";
    }

    productDao.save(product);
    return "redirect:products/list";
}

Before submitting

After submitting

我尝试返回 ModelAndView 而不是 String,没有成功,尝试使用“redirect:”,但随后验证消息也不起作用。

【问题讨论】:

    标签: java spring validation spring-mvc


    【解决方案1】:

    而不是像这样返回

    if(result.hasErrors()) {
        return "products/form";
    }
    

    加回列表对象

    if(result.hasErrors()) {
    ModelAndView modelAndView = new ModelAndView("products/form");
    modelAndView.addObject("types",PriceType.values());
    return "products/form";
     }
    

    你会拿回列表

    【讨论】:

      【解决方案2】:

      为了使您的@Valid 注释可以工作,您应该将 javax.validation 库及其实现(例如休眠)放入类路径。还要对 Product 设置一些验证约束。

      例如在 maven 中,我会将这两个库作为依赖项:

      <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.0.Final</version>
      </dependency>
      

      在产品类中:

      public class Product {
        @Min(2)
        @Max(255)
        private String title;
      }
      

      【讨论】:

      • 谢谢,但验证确实有效,我的 pom 中有这些依赖项,我的班级中有验证标签。问题是通过我添加的列表创建的字段ModelAndView 作为一个对象,在验证完成并且页面“刷新”后不会被创建......
      • 也许你可以在这里分享代码或添加更多描述。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2012-06-02
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      相关资源
      最近更新 更多