Dec 17th, 2008, 09:42 PM
#1
Error Messages are not displayed in JSP
Hi,
I'm new to Spring. I have written a simple example by using the MultiActionController. Once the user submits the login page, im validating the userName and password in validator class. Im calling validator class validate method from a controller. Validations works perfectly and im able to print number of errors in controller, but once its directed to login JSP, its not showing the error messages on the screen. I will appreciate If somebody help me in this regard..thanks....
Controller Method :
public ModelAndView onClickLogin(HttpServletRequest request,
HttpServletResponse response, LoginCommand loginCommand)
throws Exception {
/*
* BindException errors = new BindException(loginCommand,
* "loginCommand"); loginFormValidator.validate(loginCommand, errors);
*/
// this.preBind(request, loginCommand);
ServletRequestDataBinder binder = createBinder(request, loginCommand);
binder.bind(request);
BindException errors = new BindException(binder.getBindingResult());
if (loginFormValidator.supports(loginCommand.getClass ())) {
ValidationUtils.invokeValidator(loginFormValidator , loginCommand,
errors);
}
if (errors.hasErrors()) {
System.out.println("--------it has some errors21"
+ errors.getErrorCount());
ModelAndView modelAndView = new ModelAndView("login");
modelAndView.addObject("loginCommand", loginCommand);
modelAndView.addAllObjects(errors.getModel());
return modelAndView;
}
// LoginCommand loginCommand = (LoginCommand) command;
System.out.println("Inside the onClickLogin Method");
authenticationService.authenticate(loginCommand);
AccountDetail accountdetail = accountServices
.getAccountSummary(loginCommand.getUserId());
ModelAndView modelAndView = new ModelAndView("accountdetail");
modelAndView.addObject("accountdetail", accountdetail);
return modelAndView;
}
Validator Class
public class LoginFormValidator implements Validator {
public void validate(Object command, Errors errors) {
LoginCommand loginCommand = (LoginCommand) command;
// checking userName or password is empty
if ("".equals(loginCommand.getUserId().trim())) {
errors.rejectValue("userId", "error.userId.empty","UserId should not be empty");
}
if ("".equals(loginCommand.getPassword().trim())) {
errors.rejectValue("password", "error.password.empty", "Password should not be empty");
}
}
public boolean supports(Class clazz) {
return LoginCommand.class.isAssignableFrom(clazz);
}
}
JSP- Some part
<form:form method="post" commandName="loginCommand" >
<spring:bind path="loginCommand">
<c:forEach items="${status.errorMessage}" var="errorMessage">
<font color="red"> <c:out value="${errorMessage}" />
<br> </font>
</c:forEach>
</spring:bind>
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0"
cellpadding="5">
<tr>
<td alignment="right" width="20%">
User id:
</td>
<spring:bind path="loginCommand.userId">
<td width="20%">
<input type="text" name="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>"> <form:errors path="userId" />
</td>
</spring:bind>
<td width="60%">
</tr>
<tr>
<td alignment="right" width="20%">
Password:
</td>
<spring:bind path="loginCommand.password">
<td width="20%">
<input type="password"
name="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>"> <form:errors path="password" />
</td>
</spring:bind>
<td width="60%">
</tr>
xml entry
<bean id="loginBankController"
class="springexample.contoller.LoginBankController ">
<property name="authenticationService">
<ref bean="authenticationService" />
</property>
<property name="accountServices">
<ref bean="accountServices" />
</property>
<property name="methodNameResolver">
<ref bean="springBankingMethodResolver" />
</property>
<property name="loginFormValidator">
<ref bean="loginFormValidator" />
</property>
</bean>
I have attached the files also. Please refer...
Attached Files
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules