0

I am trying to run a validator class for my Spring form, but my errors are not showing up on my page. I'm a newbie when it comes to Spring validation, so there are undoubtedly some errors here. Here is my form. (Incidentally, Eclipse is posting warnings on the form:error lines that say, "List is a raw type. References to generic type List should be parameterized.")

<form:form commandName="bulletin" method="post" action="processBulletin">
    <table>
        <tr>
            <td>Name:</td>
            <td><form:errors path="name" /></td>
            <td><form:input path="name" maxlength="30" /></td>
        </tr>
        <tr>
            <td>Subject:</td>
            <td><form:errors path="subject" /></td>
            <td><form:input path="subject" maxlength="50" /></td>
        </tr>
        <tr>
            <td valign="top">Message:</td>
            <td><form:errors path="note" /></td>
            <td><form:textarea path="note" cols="70" rows="20" /></td>
        </tr>
        <tr>
            <td><input type="submit" /></td>
            <td>&nbsp;</td>
        </tr>
    </table>
</form:form>

Here is my controller class. I'm calling the validation class from here, and I'm not sure if that's the right thing to do, so feel free to say so if it's not.

@RequestMapping(value = "/processBulletin", method = RequestMethod.POST)
public String processBulletin(
        @ModelAttribute("bulletin") Bulletin bulletin, BindingResult result) {
    final BindException errors = new BindException(bulletin, "bulletin");

    bulletinValidator.validate(bulletin, errors);
    if (errors.hasErrors()) {
        return "redirect:/approvedBulletins";
    } else {
        // rest of method
    }

    return "redirect:/approvedBulletins";
}

Here is my validation class.

    @Override
    public boolean supports(Class<?> cls) {
        return Bulletin.class.isAssignableFrom(cls);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "subject", "Subject is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "Name is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "note", "Message is required");
    }
}

1 Answers1

0

Try to return view name instead of redirect return "redirect:/approvedBulletins"; when you have errors.

Alex
  • 11,451
  • 6
  • 37
  • 52
  • This didn't work. approvedBulletins is actually an action for a method I want to run, not an actual view. –  Apr 18 '13 at 16:02
  • When you use redirection, errors will be lost. But if you return the same view they should be displayed. – Alex Apr 18 '13 at 23:12
  • Okay, but approvedBulletins isn't the actual view. It's the URL of the RequestMapping for the method that returns the view I want. Should I put the actual view in? –  Apr 18 '13 at 23:38
  • See similar question here: http://stackoverflow.com/questions/15778913/spring-mvc-how-to-redirect-to-a-page-with-error OR if you really need to use redirection, you should use flash attributes (spring >= 3.1). See similar question http://stackoverflow.com/questions/2543797/spring-redirect-after-post-even-with-validation-errors – Alex Apr 19 '13 at 00:13