开发者

Spring MVC 3 Validation will not work when attributes added to model

I'm trying to get Spring annotation based validation working in a simple webapp. I'm using Spring 3.0.5, Tiles 2.2.2. In one specific case I can get the validation error to appear next to the field, however as soon as I add any objects to the model it stops working. Ideally, after the POST, I wa开发者_开发技巧nt to redirect to a GET of the form with the validation errors included. This is the setup:

I have a simple domain object.

public class DomainObject {

    @NotEmpty
    private String name;    
    private Date created;   
    private Date lastModified;
...
}

I have a controller with a GET method which adds all existing DomainObjects to the model and returns a view that displays them, and contains a very simple form for creating them. It also has a POST method for creating a new DomainObject.

@Controller
@RequestMapping("/")
public class DomainObjectController {

    @Autowired
    private DomainObjectService domainObjectService;

    @RequestMapping("form.htm")
    public String home(Model model) {

        model.addAttribute("objects", domainObjectService.getAll());
        model.addAttribute(new DomainObject());
        return "form";
    }

    @RequestMapping(value="new_object.do", method=RequestMethod.POST)
    public String newObject(@Valid DomainObject domainObject, BindingResult bindingResult, Model model) {

        if (bindingResult.hasErrors()) {
            //model.addAttribute("objects", domainObjectService.getAll());
            //model.addAttribute(new DomainObject());
            return "form";
        }
        domainObjectService.saveNew(domainObject);
        model.addAttribute("objects", domainObjectService.getAll());
        model.addAttribute(new DomainObject());
        return "form";
}
}

Here's the view:

<form:form commandName="domainObject" action="new_object.do" method="post>
<spring:message code="name" />: <form:input path="name" /> 
<input type="submit" value="<spring:message code="create.object"/>" /><form:errors path="name" cssClass="error"/></form:form>

</div>

<table class="centered">
<col width=50 />
<col width=225 />
<col width=200 />
<col width=200 />
<thead>
    <tr>
        <td id="id" class="sortable"><spring:message code="id" /></td>
        <td id="name" class="sortable"><spring:message code="name" /></td>
        <td id="created" class="sortable"><spring:message code="created" /></td>
    </tr>
</thead>
<tbody>
    <c:forEach var="obj" items="${objects}">
        <tr>
            <td class="id">${obj.id}</td>
            <td>${obj.name}</td>
            <td>${obj.created}</td>
        </tr>
    </c:forEach>
</tbody>
</table>

With this setup, if I leave the name field empty, the validation error is picked up and correctly displayed to the right of the field. However, the table is always empty, as no objects are added to the model. If I add objects to the model, by uncommenting the lines

//model.addAttribute("objects", domainObjectService.getAll());
//model.addAttribute(new DomainObject());

the table is populated but the validation error no longer appears. I can't work this one out.

As another unwanted side effect, any relative links I have in the view now no longer work (for example, a link which changes the locale href="?lang=de").

So, what could be causing the validation message to disappear when I add data to the model? And is it possible for me to redirect to the original form while keeping hold of the validation messages?

Thanks,

Russell


The validation error is attached to the object that is invalid. If you replace the object that is invalide by an new one:

model.addAttribute(newDomainObject());

then the error messages is not attached to this object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜