Wie kann ich die Anzeige einer benutzerfreundlichen Fehlermeldung, wenn der Frühling nicht in der Lage ist, zu validieren ein Modell ist java.util.Datum-Feld?
Ich bin mit Spring 3.1.2.RELEASE. Ich will eine Fehlermeldung angezeigt, die auf meiner JSP-wenn mein Feld "Datum" ist nicht korrekt formatiert. Ich dachte, ich würde alle Folgen Sie die richtigen Schritte. Ich binder a-Wandler in meinem controller ...
@InitBinder
public void initBinder(final WebDataBinder binder) {
final DateFormat dateFormat = new SimpleDateFormat(Contract.DATE_FORMAT);
dateFormat.setLenient(false);
//true passed to CustomDateEditor constructor means convert empty String to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
...
}
Habe ich diese Fehlermeldungen in meinen Nachrichten.Eigenschaften-Datei (enthalten in der Spring application context)
typeMismatch.activationDate=The activation date format should be of the form MM/dd/yyyy
typeMismatch.sampleUserForm.activationDate=The activation date format should be of the form MM/dd/yyyy
Hier ist das Modell das ich verwende:
public class SampleUserForm
{
private String userId;
private String firstName;
private String middleName;
private String lastName;
private String username;
private String url;
private String password;
private String confirmPassword;
private State state;
private java.util.Date activationDate;
private java.util.Date expirationDate;
private List<Product> products;
private Set<Role> roles = new HashSet<Role>();
Und hier ist die Fehlermeldung die ich erhalte, wenn ich meine form mit ein schlecht formatiertes Datum ...
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'sampleUserForm' on field 'activationDate': rejected value [1900]; codes [typeMismatch.sampleUserForm.activationDate,typeMismatch.activationDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sampleUserForm.activationDate,activationDate]; arguments []; default message [activationDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'activationDate'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1900"]
org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
Was muss ich sonst noch tun/prüfen, um die Falle für ein schlecht formatiertes Datum? Ich würde es vorziehen, um die Anzeige einer freundlichen Fehlermeldung an den Benutzer, anstatt die Seite gracelessly sterben.
Hier ist die controller-Methode, die handhaben soll, die form. Beachten Sie, dass ich bereits im Lieferumfang enthalten eine BindingResult.
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(final HttpServletRequest request,
final SampleUserForm sampleUserForm,
final Model model,
final BindingResult result)
{
String nextPage = "sampleusers/add";
m_sampleUserFormValidator.validate(sampleUserForm, result);
if (!result.hasErrors())
{
... process the model and determine the next page ...
} //if
return new ModelAndView(nextPage);
}
Du musst angemeldet sein, um einen Kommentar abzugeben.
Nehme ich Sie verbindlich sein sollte die form in einem Ihrer
POST
Methoden mit@ModelAttribute
. In der gleichen Methode binden eineBindingResult bindingResult
und alle die binding-Fehler erfasst werden soll, in diesembindingResult
Objekt. Innerhalb der Methode, sollten Sie in der Lage sein, zu prüfen,if (bindingResult.hasErrors()) {
und entsprechende Maßnahmen ergreifen.
Finden Sie unter: Spring @Gültig validator nicht richtig aufgerufen (Roo, Hibernate).
Kurzen Geschichte ist das ändern der controller-Methode Signatur von
zu
festen alles.
Ich glaube, dass der Fehler ist, wenn Sie Formate der Daten. Um zu bestätigen, dass müssen Sie die Anwendung Debuggen und überprüfen Sie das Datum Wert. Und noch eine gute übung wäre, zu glauben, dass Sie konfigurieren Sie eine standard-format für Ihre Anwendung.
Folgen Sie dem Beispiel unten:
kommentieren eine Methode auf, dass die controller-Klasse mit dieser Klasse/Methode level annotation:
mehr info
Können Sie unter diesem link für mehr
http://forum.springsource.org/showthread.php?58933-Handle-excpetions-from-custom-property-editor-spring-2-5
http://www.codercorp.com/blog/spring/registering-propertyeditorss-with-spring-for-custom-objects-example.html
BindingResult Argument sollte neben Modell validiert wird. ZB