我在spring 3 mvc应用程序中遇到表单验证问题。
似乎所有内容都已设置好,但是当表单字段中存在错误条目时,验证程序会检测到错误条目,并且不会显示任何错误消息。
这是验证器
@Component
public class SettingsValidator implements Validator{
@Override
public boolean supports(Class<?> clazz) {
return VrSettings.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "applicationBaseUri", "field.required", "field required");
ValidationUtils.rejectIfEmpty(errors, "remoteDnsServiceUri","field.required", "field required");
VrSettings settings = (VrSettings) target;
try
{
URI uri = new URI(settings.getApplicationBaseUri());
}
catch (URISyntaxException e)
{
errors.rejectValue("applicationBaseUri", "error.invalid.uri", "invalid uri");
}
try
{
URI uri = new URI(settings.getLocalApplicationBaseURI());
}
catch (URISyntaxException e)
{
errors.rejectValue("localApplicationBaseURI", "error.invalid.uri", "invalid uri");
}
try
{
URI uri = new URI(settings.getRemoteDnsServiceUri());
}
catch (URISyntaxException e)
{
errors.rejectValue("remoteDnsServiceUri", "error.invalid.uri", "invalid uri");
}
}
}
控制器......
@Controller
public class UserInterfaceController {
@InitBinder
protected void initBinder(WebDataBinder binder)
{
binder.setValidator(new SettingsValidator());
}
@RequestMapping(method=RequestMethod.POST, value="ui/settings")
public ModelAndView postSettings(@ModelAttribute("settings") @Valid VrSettings settings, BindingResult result)
{
ModelAndView mav = new ModelAndView("settings");
mav.addObject("settings", settings);
if(result.hasErrors())
{
mav.addAllObjects(result.getModel());
return mav;
}
LinkedList<VrSettings> vrSettingsList = persistenceManager.getAll(new VrSettings());
if(vrSettingsList.isEmpty())
{
//do things
}
else
{
//do things
}
return mav;
}
}
最后是JSP
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<h3>Configuration Parameters</h3>
<span style="font-weight:bold; color: red">
<form:errors path="*" />
</span><br />
<div class="form">
<form:form method="post" action="settings" commandName="settings">
<form:label for="baseuri" path="applicationBaseUri">base URI*</form:label>
<form:input class="text" path="applicationBaseUri" size="35" name="baseuri"/>
<form:label for="localbaseuri" path="localApplicationBaseURI">local base URI</form:label>
<form:input class="text" path="localApplicationBaseURI" size="35" name="localbaseuri"/>
<form:label for="ldns" path="remoteDnsServiceUri">ldns URI*</form:label>
<form:input class="text" path="remoteDnsServiceUri" size="35" name="ldns"/>
<div class="form-row">
<input type="submit" value="submit"/>
</div>
</form:form>
</div>
</body>
我无法理解我所缺少的东西。 感谢任何帮助,提前谢谢!
答案 0 :(得分:2)
<form:errors path="*" />
需要知道它应该对哪个命令对象进行操作。所以将此标记移到
中<form:form method="post" action="settings" commandName="settings">
如果没有这个,org.springframework.web.servlet.tags.form.AbstractHtmlElementBodyTag.shouldRender()
的方法org.springframework.web.servlet.tags.form.ErrorsTag
只会返回SKIP_BODY
,因为它无法为您的命令对象找到BindingResult
。