Java Spring3表单验证器
我在Spring3MVC应用程序中遇到了表单验证问题Java Spring3表单验证器,java,spring,jsp,spring-mvc,validation,Java,Spring,Jsp,Spring Mvc,Validation,我在Spring3MVC应用程序中遇到了表单验证问题 <h3>Configuration Parameters</h3> <span style="font-weight:bold; color: red"> <form:errors path="*" /> </span><br /> <div class="form"> <f
<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>
看起来一切都设置好了,但当表单字段中插入错误条目时,验证程序会检测到它,并且不会显示错误消息
<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>
这是验证器
@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");
}
}
<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>
@组件
公共类设置验证程序实现验证程序{
@凌驾
公共布尔支持(类clazz){
返回VrSettings.class.isAssignableFrom(clazz);
}
@凌驾
公共无效验证(对象目标、错误){
ValidationUtils.rejectIfEmpty(错误,“applicationBaseUri”、“field.required”、“field required”);
ValidationUtils.rejectIfEmpty(错误,“remoteDnsServiceUri”、“field.required”、“field required”);
VrSettings=(VrSettings)目标;
尝试
{
URI=新URI(settings.getApplicationBaseUri());
}
捕获(URISyntaxException e)
{
errors.rejectValue(“applicationBaseUri”、“error.invalid.uri”、“invalid uri”);
}
尝试
{
URI=新URI(settings.getLocalApplicationBaseURI());
}
捕获(URISyntaxException e)
{
errors.rejectValue(“localApplicationBaseURI”、“error.invalid.uri”、“invalid uri”);
}
尝试
{
URI=新URI(settings.getRemoteDnsServiceUri());
}
捕获(URISyntaxException e)
{
errors.rejectValue(“remoteDnsServiceUri”、“error.invalid.uri”、“invalid uri”);
}
}
}
<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>
控制器
@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;
}
}
<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>
@控制器
公共类UserInterfaceController{
@InitBinder
受保护的活页夹(WebDataBinder活页夹)
{
binder.setValidator(新的SettingsValidator());
}
@RequestMapping(method=RequestMethod.POST,value=“ui/settings”)
public ModelAndView postSettings(@modeldattribute(“settings”)@有效的VrSettings设置,BindingResult)
{
ModelAndView mav=新的ModelAndView(“设置”);
mav.addObject(“设置”,设置);
if(result.hasErrors())
{
mav.addAllObject(result.getModel());
返回mav;
}
LinkedList vrSettingsList=persistenceManager.getAll(新的VrSettings());
if(vrSettingsList.isEmpty())
{
//做事
}
其他的
{
//做事
}
返回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>
<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>
配置参数
基本URI*
本地基URI
ldns URI*
<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>
我不明白我错过了什么。
感谢您的帮助,提前谢谢
<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>
<form:errors path="*" />
需要知道它应该在哪个命令对象上操作。所以把这个标签移到里面
<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>
<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
<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>