spring version 2.0.7
Want to show error messages on login page but it is failing. I don't know y even error message from validate method of loginValidator is failing to show on jsp,
It is catching exception or errors but fail to show error messages.
1) Bean definition
2) ControllerHTML Code:<bean class="com.fulcrum.mvc.controller.LoginController" id="loginController"> <property name="formView"> <value>loginUser</value> </property> <property name="successView"> <value>welcome</value> </property> <property name="userService"> <ref bean="userService" /> </property> <property name="ldapService"> <ref bean="ldapService"/> </property> <property name="commandClass"> <value>com.fulcrum.model.dto.UserDto</value> </property> <property name="validator"> <bean class="com.fulcrum.mvc.controller.validator.LoginValidator"/> </property> </bean>
3) JSPCode:public class LoginController extends SimpleFormController { public LoginController() { setCommandClass(UserDto.class); } protected Object formBackingObject(HttpServletRequest req) throws Exception { return new UserDto(); } protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { UserDto userDto = (UserDto) command; try { userDto = getLdapService().findUser(userDto.getUsername(),userDto.getPassword()); if (userDto != null) { userDto = getUserService().findById(userDto); } } catch (UsernameNotFoundException ne) { errors.reject("error.invalide"); return showForm(request, response, errors); } catch (AuthenticationException ne) { errors.reject("error.invalide"); return showForm(request, response, errors); } catch (FulcrumException ne) { errors.reject("error.invalide"); return showForm(request, response, errors); } catch (Exception ne) { errors.reject("error.invalide"); return showForm(request, response, errors); } return new ModelAndView(getSuccessView(), "userDto", userDto); } // COLLABORATORS private UserService userService; private LDAPService ldapService; /** * @return the ldapService */ public LDAPService getLdapService() { return ldapService; } /** * @return the userService */ public UserService getUserService() { return userService; } /** * @param userService * the userService to set */ public void setUserService(UserService userService) { this.userService = userService; } /** * @param ldapService * the ldapService to set */ public void setLdapService(LDAPService ldapService) { this.ldapService = ldapService; } }
4) ValidatorHTML Code:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> <%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="POST" action="login.htm"> <table border="0" cellspacing="5" cellpadding="0"> <tr> <td align="right" valign="middle"><b>Login: </b></td> <td align="left" valign="middle"> <input type="text" name="username" value="" > </td> </tr> <tr> <td align="right" valign="middle"><b>Password: </b></td> <td align="left" valign="middle"> <input type="password" name="password" value="" > </td> </tr> <tr> <td colspan="2" align="left"><input type="submit" value=" Log In "></td> </tr> </table> </form> <spring:hasBindErrors name="errors"> <p>There were ${errors.errorCount} errors</p> <c:forEach var="err" items="${errors.allErrors}"> <spring:message code="${err.code}" arguments="${err.arguments[0]}"/> </c:forEach> </spring:hasBindErrors> </body> </html>
5) propertiesCode:public class LoginValidator extends BaseValidator { @Override public void validate(Object command, Errors errors) { UserDto user = (UserDto) command; ValidationUtils.rejectIfEmpty(errors, "username", "error.required.loginid", new Object[]{user.getUsername()},"Login is required"); ValidationUtils.rejectIfEmpty(errors, "password", "error.required.passwd","Password is required"); } @Override public boolean supports(Class clazz) { return clazz.equals(UserDto.class); } }
error.required.loginid=Login is required {0}
error.required.passwd=Password is required
error.invalide=Authetication failed


Reply With Quote

