如何显示表单中每个字段的错误消息? [英] How I can show error message for each field in form?

查看:121
本文介绍了如何显示表单中每个字段的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的验证器中,我为每个字段添加了一些描述,并希望以表单(.jsp)显示错误消息,但是当我为每个字段显示消息时,错误消息都是一样的,这是什么问题,我的代码: /p>

验证器

public class StoreValidator implements Validator {

@Override
public boolean supports(Class<?> clazz) {
    return Store.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.empty", "Name field is empty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "address.empty", "Address field is empty");
}

}

控制器

    @Autowired
private StoreValidator storeValidator;

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(storeValidator);
}

//post method create store
@RequestMapping(value = "/regStoreSuccessful", method = RequestMethod.POST)
public ModelAndView addStorePost(@Valid @ModelAttribute("storeForm") Store storeForm, BindingResult bindingResult, Principal principal, RedirectAttributes redirectAttrs) throws SQLException {
    ModelAndView modelAndView = new ModelAndView("redirect:body");

    storeValidator.validate(storeForm,bindingResult);

    if(bindingResult.hasErrors()) {
        redirectAttrs.addFlashAttribute("error", bindingResult.getFieldError().getDefaultMessage());
        //modelAndView.addObject("storeForm", bindingResult.getFieldError().getDefaultMessage());
  //modelAndView.addObject("storeForm", storeForm);

  //redirectAttrs.addFlashAttribute("errors", bindingResult.getFieldError().getRejectedValue());
        //redirectAttrs.addFlashAttribute("storeForm", storeForm);

    //..
    }
    //..
    }

表格

<form:form method="POST" action="/regStoreSuccessful" commandName="storeForm">
  <table>
  <tr>
    <td><form:label path="name">Name store</form:label></td>
    <td><form:input path="name" /></td>
    <td><form:errors path="name" /></td>
  </tr>
   <tr>
    <td><form:label path="address">Address store</form:label></td>
    <td><form:input path="address" /></td>
    <td><form:errors path="address" /></td>
  </tr>
  </form:form>

我也尝试使用<form:errors path = "name" />,但是不起作用...

解决方案

您应该在hasErrors方法的model属性中添加整个对象 对于每个输入,请分别使用form:errors.

<form:errors path = "name" />
<form:errors path = "address" />

if(bindingResult.hasErrors()) {
    model.addAttribute("storeForm",storeForm);
//..
}

In my validator I add some description for each field, and want to display error message in form(.jsp), but when I display message for each field, the error messages is the same, what's problem, my code:

Validator

public class StoreValidator implements Validator {

@Override
public boolean supports(Class<?> clazz) {
    return Store.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.empty", "Name field is empty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "address.empty", "Address field is empty");
}

}

Controller

    @Autowired
private StoreValidator storeValidator;

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(storeValidator);
}

//post method create store
@RequestMapping(value = "/regStoreSuccessful", method = RequestMethod.POST)
public ModelAndView addStorePost(@Valid @ModelAttribute("storeForm") Store storeForm, BindingResult bindingResult, Principal principal, RedirectAttributes redirectAttrs) throws SQLException {
    ModelAndView modelAndView = new ModelAndView("redirect:body");

    storeValidator.validate(storeForm,bindingResult);

    if(bindingResult.hasErrors()) {
        redirectAttrs.addFlashAttribute("error", bindingResult.getFieldError().getDefaultMessage());
        //modelAndView.addObject("storeForm", bindingResult.getFieldError().getDefaultMessage());
  //modelAndView.addObject("storeForm", storeForm);

  //redirectAttrs.addFlashAttribute("errors", bindingResult.getFieldError().getRejectedValue());
        //redirectAttrs.addFlashAttribute("storeForm", storeForm);

    //..
    }
    //..
    }

Form

<form:form method="POST" action="/regStoreSuccessful" commandName="storeForm">
  <table>
  <tr>
    <td><form:label path="name">Name store</form:label></td>
    <td><form:input path="name" /></td>
    <td><form:errors path="name" /></td>
  </tr>
   <tr>
    <td><form:label path="address">Address store</form:label></td>
    <td><form:input path="address" /></td>
    <td><form:errors path="address" /></td>
  </tr>
  </form:form>

Also I tried used <form:errors path = "name" />, but isn't working...

解决方案

You should add entire object in model attribute in hasErrors method and for each input separately use form:errors for each input.

<form:errors path = "name" />
<form:errors path = "address" />

if(bindingResult.hasErrors()) {
    model.addAttribute("storeForm",storeForm);
//..
}

这篇关于如何显示表单中每个字段的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆