I am working on a Spring 3 MVC application with form validation. The validation is w开发者_JS百科orking on the server side but even though I am returning the BindingResult the tag doesn't show a message when validation fails.
Controller Method:
@RequestMapping(value = "server/{serverId}/save", method = RequestMethod.POST)
public ModelAndView saveServer( @PathVariable long serverId,
@Valid ServerEditor serverEditor,
BindingResult result){
AdminSystemServer server = adminService.loadServer(serverId);
if (!result.hasErrors()){
server.setServerName(serverEditor.getServerName());
server.setServerUrl(serverEditor.getServerUrl());
adminService.save(server);
}
mv.setViewName(".layout.servers.manage.server");
mv.addObject("server", server);
mv.addObject("result", result);
return mv;
}
Form jsp
<form:form commandName="serverEditor" action="/admin/app/servers/manage/system/save">
<span class="tableRow">
<label for="serverName">System Name</label>
<form:input path="serverName" class="required" /><br />
</span>
<span class="tableRow">
<form:errors cssClass="errors" path="serverName" />
</span>
</form:form>
I know the validation is working, as I step through debug and see the errors in the binding result, but the messages never appear.
Anyone have any ideas?
Try adding
@ModelAttribute("commandName"),
as a method parameter after @PathVariable long serverId,
Also can you post more of your jsp?
In your sample code you are displaying error for "systemName" while bound bean has property "serverName"...
Turns out my problem, which someone asked about earlier but I didn't think was connected, was having the ModelAndView declared on the class level. This prevents the bindingresult from being properly inserted into the model.
精彩评论