当表单绑定到列表时,Spring MVC@Valid无效

当表单绑定到列表时,Spring MVC@Valid无效,spring,validation,Spring,Validation,我试图在绑定到对象列表的SpringMVC表单上进行表单验证。验证不起作用。如果我遗漏了什么,请告诉我 @Component public class Customer{ @NotEmpty private int custId; @NotEmpty private List<Order> orders; //Getters & Setters... } @Component public class Order{ @NotEmpty private

我试图在绑定到对象列表的SpringMVC表单上进行表单验证。验证不起作用。如果我遗漏了什么,请告诉我

@Component
public class Customer{
   @NotEmpty private int custId;
   @NotEmpty private List<Order> orders;
   //Getters & Setters...
}

@Component
public class Order{
   @NotEmpty private String id;
   @NotEmpty private String orderName;
   //Getters & Setters...
}

//JSP - custFormBean is set as a model attribute

<form:form method="post" action="/submitOrder.htm" modelAttribute="custFormBean">
    <table class="activity" width="600px" bgcolor="#FCF4DE">
        <c:forEach items="${custForm.orders}" var="order" varStatus="status">
            <tr>
                <td>
                    <c:out value="${order.id}" />
                </td>
            </tr>
            <tr>
                <td>                    
                    <form:password path="orders[${status.index}].orderName" name="name" />
                </td>
                <td><form:errors path="orders[${status.index}].orderName" cssClass="errorMessage" /></td> 
            </tr>
        </c:forEach>
        <tr></tr>
        <tr>
            <td align="center" colspan="2">
                <input type="submit" id="orderSubmit" class="formButton" value="OK" />
            </td>
        </tr>
    </table>
</form:form>

//Controller
@Controller
@SessionAttributes("custFormBean")
public class CustomerController {

    @RequestMapping(value = "/order.htm", method=RequestMethod.GET)
    public String getOrder(ModelMap model, HttpServletRequest request) {
        nextPage = "order"
        try {
            Customer custBean = custService.getCustOrders(...);
            model.addAttribute("custFormBean", custBean); 
        } catch (ServiceException e) {
            log.error("ServiceException when calling getChallengeQuestionLists", e);
        }       
        return nextPage;
    }

   @RequestMapping(value = "/submitOrder.htm", method=RequestMethod.POST)
   public String submitOrder(@Valid @ModelAttribute("custFormBean") Customer custBean, BindingResult result, Model model, HttpServletRequest request){
        String nextPage = "success";
    if(result.hasErrors()) {        
//This is not working - The custBean is populated with the values entered in the form but if I leave the fields empty the validation is not kicking in.
       log.debug("Validation errors...");
       nextPage = "error";
    }   
    return nextPage;
   }
}
@组件
公共类客户{
@NotEmpty private int custId;
@非空私有列表订单;
//接球手和接球手。。。
}
@组成部分
公共阶级秩序{
@NotEmpty私有字符串id;
@NotEmpty私有字符串orderName;
//接球手和接球手。。。
}
//JSP-custFormBean被设置为模型属性
//控制器
@控制器
@会期贡献(“custFormBean”)
公共类客户控制器{
@RequestMapping(value=“/order.htm”,method=RequestMethod.GET)
公共字符串getOrder(ModelMap模型,HttpServletRequest){
nextPage=“订单”
试一试{
Customer-custBean=custService.getCustOrders(…);
addAttribute(“custFormBean”,custBean);
}捕获(服务异常e){
log.error(“调用GetChallengeQuestionList时出现ServiceException”,e);
}       
返回下一页;
}
@RequestMapping(value=“/submitOrder.htm”,method=RequestMethod.POST)
公共字符串提交器(@Valid@modeldattribute(“custFormBean”)客户custBean、BindingResult、模型模型、HttpServletRequest){
字符串nextPage=“成功”;
if(result.hasErrors()){
//这不起作用-custBean由表单中输入的值填充,但如果我将字段留空,则验证不会生效。
调试(“验证错误…”);
nextPage=“错误”;
}   
返回下一页;
}
}
如果我缺少以下内容,请通知我:

@Component
public class Customer{
   @NotEmpty private int custId;
   @NotEmpty private List<Order> orders;
   //Getters & Setters...
}

@Component
public class Order{
   @NotEmpty private String id;
   @NotEmpty private String orderName;
   //Getters & Setters...
}

//JSP - custFormBean is set as a model attribute

<form:form method="post" action="/submitOrder.htm" modelAttribute="custFormBean">
    <table class="activity" width="600px" bgcolor="#FCF4DE">
        <c:forEach items="${custForm.orders}" var="order" varStatus="status">
            <tr>
                <td>
                    <c:out value="${order.id}" />
                </td>
            </tr>
            <tr>
                <td>                    
                    <form:password path="orders[${status.index}].orderName" name="name" />
                </td>
                <td><form:errors path="orders[${status.index}].orderName" cssClass="errorMessage" /></td> 
            </tr>
        </c:forEach>
        <tr></tr>
        <tr>
            <td align="center" colspan="2">
                <input type="submit" id="orderSubmit" class="formButton" value="OK" />
            </td>
        </tr>
    </table>
</form:form>

//Controller
@Controller
@SessionAttributes("custFormBean")
public class CustomerController {

    @RequestMapping(value = "/order.htm", method=RequestMethod.GET)
    public String getOrder(ModelMap model, HttpServletRequest request) {
        nextPage = "order"
        try {
            Customer custBean = custService.getCustOrders(...);
            model.addAttribute("custFormBean", custBean); 
        } catch (ServiceException e) {
            log.error("ServiceException when calling getChallengeQuestionLists", e);
        }       
        return nextPage;
    }

   @RequestMapping(value = "/submitOrder.htm", method=RequestMethod.POST)
   public String submitOrder(@Valid @ModelAttribute("custFormBean") Customer custBean, BindingResult result, Model model, HttpServletRequest request){
        String nextPage = "success";
    if(result.hasErrors()) {        
//This is not working - The custBean is populated with the values entered in the form but if I leave the fields empty the validation is not kicking in.
       log.debug("Validation errors...");
       nextPage = "error";
    }   
    return nextPage;
   }
}
检查字符串是否为空(非空且长度>0)或 集合(或数组)不为空(不为null且长度>0)

我认为您的列表不是空的,并且长度>0,尽管它们都是空的。因此,根据这一点,验证器可能会踢它,并产生正确的结果


您可能需要编写自定义验证器来检查列表中是否至少有一个非空元素?

是否在servlet上下文XML中添加了该元素?您希望发生什么?您使用的是什么输入?我的XML中有注释。我还试图验证JSP字段,并在验证失败的字段旁边显示错误消息。谢谢@gerrytan。我可以为此编写自定义验证器,但现在的问题是在字段旁边显示验证错误。例如,如果orderNum为空,则验证程序只将路径解析为“orderNum”,而不是“customer[0].orderNum”。我可以用path=“*”在顶部显示验证消息,但我希望在验证失败的字段旁边显示消息。任何帮助都将不胜感激。