2012-01-04 59 views
1

我在spring 3 mvc應用程序中對錶單驗證有問題。Spring 3表單驗證程序

似乎一切都設置好了,但是當一個錯誤的條目發送到表單字段時,它會被驗證器檢測到,並且不會顯示錯誤消息。

這裏的驗證

@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> 

我不明白我錯過了什麼。 任何幫助表示讚賞,在此先感謝!

回答

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你的命令對象。

+0

我很感激,你解決了我的問題! – MaVVamaldo 2012-01-05 08:21:09