PDA

View Full Version : Problem with displaying Validation error in JSP


anjanks1981
Jun 5th, 2008, 04:34 PM
I have a Student bean with name, email and phone number. I am using Java 1.4 with RAD 6.0. There are no compilation or run time errors that I see. I have written a validator for Student and it is being accessed and error messages are displayed in the console saying that name is empty if I don't enter the name.

However, I am not able to display this in JSP although I have used <spring:bind>

//In validator
public void validate(Object command,Errors errors)
{
Student student = (Student)command;
ValidationUtils.rejectIfEmpty(errors,"name","requi red.name","Name is required");
ValidationUtils.rejectIfEmpty(errors,"email","requ ired.email","Email is required");
ValidationUtils.rejectIfEmpty(errors,"phoneNumber" ,"required.phoneNumber","Phone Number is required");

}

In JSP,
<spring:bind path="student.name">
<input type="text" name="name" value="${status.value}" />
</spring:bind>

<spring:hasBindErrors name="student">
<font color="red">${status.errorMessage}</font>
</spring:hasBindErrors>

In configuration file for Spring
<bean id="getSubjectsController"
class="com.assurant.GetSubjectsController">
<property name="formView">
<value>InputName</value>
</property>
<property name="successView">
<value>OutputSubjects</value>
</property>
<property name="commandName"><value>student</value></property>
<property name="commandClass"><value>com.assurant.Student</value></property>
<property name="validator">
<bean class="com.assurant.StudentValidator" />
</property>
</bean>

I see no errors and Validation is being accessed correctly when I don't enter anything for "name". But why the error message which I see on console is not visible in JSP?

I had followed the link posted[/url] on the forum..but not working

some one
Jun 5th, 2008, 04:45 PM
<spring:bind path = "student.*">
<c:forEach items="${status.errorMessages}" var="error">
Error code: <c:out value="${error}"/><br>
</c:forEach>
</spring:bind>


This piece of code works for me

anjanks1981
Jun 5th, 2008, 05:05 PM
Thanks for the coded, but is still not working. I just pasted the same in my JSP.

<form name="inputName" action="getSubjects.htm" method="POST">
<spring:bind path = "student.*">
<c:forEach items="${status.errorMessages}" var="error">
Error code: <c:out value="${error}"/><br>
</c:forEach>
</spring:bind>
Enter your name

<spring:bind path="student.name">
<input type="text" name="name" value="${status.value}" />
</spring:bind>

Enter email
<input type="text" name="email" value="${status.value}" /><br>
Enter phoneNumber
<input type="text" name="phoneNumber" value="${status.value}" />
<input type="submit" name="submit" />
</form>

If i enter all 3 fields, then it redirects to the correct page. If I give wrong or no input it goes back to Inputpage again. This is working correctly. I am not able to yet display messages on JSP yet

some one
Jun 5th, 2008, 05:37 PM
just a thought if you want these fields to be required then why dont you
override the initBinder function.

you can add

a line in there

binder.setRequiredFields(new String[] {"name","email", "phoneNumber" });

that sure beats writing a validator for every bean you have which would do what the above line does.

http://static.springframework.org/spring/docs/2.5.4/api/org/springframework/web/servlet/mvc/BaseCommandController.html

~s.