Results 1 to 4 of 4

Thread: Not displaying Error Messages on JSP

  1. #1

    Default Not displaying Error Messages on JSP

    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
    HTML 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>
    2) Controller

    Code:
    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;
    	}
    
    }
    3) JSP
    HTML 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>
    4) Validator
    Code:
    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);
    	}
    }
    5) properties
    error.required.loginid=Login is required {0}
    error.required.passwd=Password is required
    error.invalide=Authetication failed

  2. #2

    Default

    anyone

  3. #3
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    The "name" attribute on your hasBindErrors tag should match the commandName you've assigned to your controller (default: "command").
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  4. #4

    Default

    Quote Originally Posted by pmularien View Post
    The "name" attribute on your hasBindErrors tag should match the commandName you've assigned to your controller (default: "command").
    Thanks pmularien, that worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •