0

i am trying to persist an Entity using the sf:form.

this is the jsp:

<sf:form method="POST" action="${pageContext.request.contextPath}/addStudent" modelAttribute="student">
<fieldset>
<table>
<tr>
    <th><sf:label path="nb">Number:</sf:label></th>
    <td><sf:input type="text" path="nb"/><br/>
    <sf:errors path="nb"></sf:errors>
</tr>
......
</table>  
</fieldset>

the attribute in the entity is like that:

@NotNull(message="not null")
@Size(min=5, max=10, message="length min 5")
@Column(name="NB", unique=true)
private String nb;

the controller:

@RequestMapping(value="/addStudent", method=RequestMethod.POST)
public ModelAndView addStudent(HttpServletRequest request, @ModelAttribute("student") @Valid Student student, BindingResult bR){

    ModelAndView mav = new ModelAndView("students");
    if(bR.hasErrors()){
        return mav;
    }
    studentService.saveStudent(student);
    return mav;
}

Well, when i leave the nb field empty or the entry is to short i get a validation error. my problem is that the error is not shown in the jsp but an exception is thrown:

List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='length min 5', propertyPath=nb, rootBeanClass=class i.have.serious.problem.Student, messageTemplate='length min 5'}  
javax.validation.ConstraintViolationException: Validation failed for classes [i.have.serious.problem.Student] during persist time for groups [javax.validation.groups.Default, ]

maybe i miss something .. i appreciate any hint or help, thx

----------------------------- SOLVED -----------------------------------

2 Answers 2

1

well indeed .. i missed something -> Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.

xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<mvc:annotation-driven />

works fine now :) .. thx for caring tho!

Sign up to request clarification or add additional context in comments.

Comments

0

You are creating a new ModelAndView in the method, this may not be carrying back the bindingResult back to the view.

Can you instead take ModelAndView as an additional parameter and see if that works out:

@RequestMapping(value="/addStudent", method=RequestMethod.POST)
public ModelAndView neuStudent(HttpServletRequest request, @ModelAttribute("student") @Valid Student student, BindingResult bR, ModelAndView mav){

    mav.setViewName("students");
    if(bR.hasErrors()){
        return mav;
    }
    studentService.saveStudent(student);
    return mav;
}

1 Comment

thx for caring! problem solved as stated beneath:) kudos nevertheless!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.