All,
I'm using a simple form controller to handle a username and password submitted from a login page.PHP Code:
publicnbsp;classnbsp;LoginFormControllernbsp;extendsnbsp;SimpleFormControllernbsp;{nbsp;privatenbsp;SecurityServicenbsp;securityService;publicnbsp;ModelAndViewnbsp;onSubmit(fromServletRequestnbsp;request,nbsp;fromServletResponsenbsp;response,nbsp;Objectnbsp;command,nbsp;BindExceptionnbsp;errors){nbsp;Usernbsp;usernbsp;=nbsp;(User)nbsp;command;usernbsp;=nbsp;securityService.authenticate(user);if(user.isValid()){nbsp;fromSessionnbsp;sessionnbsp;=nbsp;request.getSession(true);session.setAttribute("user",user);returnnbsp;newnbsp;ModelAndView(newnbsp;RedirectView(getSuccessView()));}nbsp;elsenbsp;{nbsp;returnnbsp;newnbsp;ModelAndView("login","login",user);}nbsp;}nbsp;protectednbsp;Objectnbsp;formBackingObject(fromServletRequestnbsp;request)nbsp;throwsnbsp;ServletExceptionnbsp;{nbsp;Usernbsp;usernbsp;=nbsp;newnbsp;User();//usenbsp;thisnbsp;tonbsp;setnbsp;thenbsp;defaultnbsp;propertynbsp;ofnbsp;thenbsp;formnbsp;fields.nbsp;user.setUsername("");returnnbsp;user;}nbsp;publicnbsp;voidnbsp;setSecurityService(SecurityServicenbsp;securityService){nbsp;this.securityServicenbsp;=nbsp;securityService;}}nbsp;
I have a validator that checks the form input and and returns validation errors to the login page below.HTML Code:
lt;%@ include file=quot;/WEB-INF/jsp/include.jspquot; %gt;
lt;%@ taglib prefix=quot;springquot; uri=quot;/springquot; %gt;
lt;htmlgt;
lt;headgt;lt;titlegt;lt;fmt:message key=quot;titlequot;/gt;lt;/titlegt;lt;/headgt;
lt;bodygt;
lt;h1gt;lt;fmt:message key=quot;headingquot;/gt;lt;/h1gt;
lt;spring:hasBindErrors name=quot;loginquot;gt;
lt;table border=quot;1quot;gt;
lt;trgt;
lt;tdgt;Username and/or password is invalid!lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/spring:hasBindErrorsgt;
lt;pgt;lt;fmt:message key=quot;login.instructionsquot;/gt;lt;/pgt;
lt;form method=quot;postquot;gt; lt;table border=quot;0quot; cellspacing=quot;0quot; cellpadding=quot;5quot;gt; lt;trgt; lt;spring:bind path=quot;login.usernamequot;gt; lt;td width=quot;20%quot;gt; lt;input type=quot;textquot; name=quot;usernamequot; value=quot;lt;c ut value=quot;${status.value}quot;/gt;quot;gt; lt;/tdgt; lt;/spring:bindgt; lt;/trgt; lt;trgt; lt;spring:bind path=quot;login.passwordquot;gt; lt;td width=quot;20%quot;gt; lt;input type=quot;passwordquot; name=quot;passwordquot; value=quot;quot;gt; lt;/tdgt; lt;/spring:bindgt; lt;/trgt; lt;/tablegt; lt;input type=quot;submitquot; alignment=quot;centerquot; value=quot;Loginquot;gt;
lt;/formgt;
lt;/bodygt;
lt;/htmlgt;
My question is, in my controller, how to I send the user back to the login page and show the user an error. I can send them back to the form, but it errors out if I dont supply a new login object and I can seem to find a property that will trigger the hasBindErrors JSTL tag. Please let me know if I am going about this the wrong way.
You actually want to put custom validation logic in the following method.Code:
protected void onBindAndValidate(fromServletRequest request, Object command, BindException errors) throws Exception {
}
This method gets called before the onSubmit() method. If there are any errors added to the BindException object then the formView is redisplayed with the data that was submitted and an errors.
You can either directly work with the BindException object or use the ValidationUtils.
Also, check out this blog entry about pushing all validation logic into a class that implements the Validator interface. You can then register Validators on the SimpleForm controller to do this logic.
main/200...my-first-post/Hope this makes sense...
Thanks cwash! That is exactly what I needed. |