File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Application Frameworks and the fly likes Backend validation - a complete example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Backend validation - a complete example" Watch "Backend validation - a complete example" New topic

Backend validation - a complete example

Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Guys,

I've finally succeeded in getting errors added to the BindException object in my controller's onSubmit method, and having them rendered on my jsp page via the spring:hasBindErrors tag. I have to say this was not an easy task, I gathered snippets of information about doing this from various websites and my Expert Spring MVC book, no one single source had a complete example. So I thought I might show the process I took and I'd love to hear feedback on whether I'm taking the right tack on this:

My controller:

My controller overrides onSubmit(request, response, object, bindException). If the username is invalid, then an error is added to bindException and showForm is called to return the form to the user. To control the clearing of the form when re-displaying it, I overrode referenceData(request, command, errors):

I had to cast to a BindeException in order to get at the original model that was created. With this, I pulled out my UserBean object and cleared the fields.
Finally my JSP page:

This was pretty standard, the only part I thought was a little awkward was in how I had to place a spring:message lookup for the errorcode along with its argument, I thought the error objects would have this error desription ready for my at the point.
I think the part I got stumped on the most was how I had to get at the model through the bindException object in my referenceData method. Simply creating and returning a new Map with the same "user" key mapped to a new UserBean object didn't seem to work, because this new Map would be merged with the existing model object using addAll and it wouldn't overwrite the first object keyed off of "user".
Does the above look appropriate for handling logical validation in a controller?

Thanks for the help

SCJP 1.4 SCJD 1.5
Henry Lowell
Ranch Hand

Joined: May 29, 2006
Posts: 63
Holy cow! All that just to show some errors on the JSP? Spring MVC at it's finest.

Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Yeah I know. I'm a huge fan of Spring, I really want Spring MVC to work, but it's 5 times easier to do this with a forward tag in struts. Maybe that's why I struggled with this, because I was looking for the "forward tag" equivalent with Spring. It very well could be that the "perfect" MVC framework out there is struts + spring. I think I'm going to open a can of worms with that statement, but some frameworks do a better job at certain tasks than others.
Jordi Monn�

Joined: Feb 28, 2006
Posts: 21
More info here:

Joined: Jul 06, 2006
Posts: 2
hi! Mike / Jordi,

I am trying to do the same thing userid / password validation using spring MVC without much success. I am new to Spring. I have to check whether username/passwd is present in the database and get back to the form if there is error or go to the success view(displayMenu.jsp). Can you guys help me with this?
My questions: where do you check that username and password exists in the database or not? In the validator or controller? Now you are checking

if(!badUsername) {

Is badUsername being set in the validator or controller?

Thanks! Anyway this posting was very helpful to me in the first case....

Peter Oxenham

Joined: Jul 17, 2006
Posts: 1
In response to BLUE COW:

The quick answer to you question is 'either'. You can do it in a Validator or the controller.

If you don't want to create a validator, then override the onBindAndValidate method of BaseCommandController.

Simply do your check there (via your DAO) and call reject on the BindException if the username is bad.

I recommend the same to you. Do your check for badUsername in onBindAndValidate and if you do add an error then SimpleFormController will automatically display your formView for you. You could also blank out the email and password fields at that point, instead of in referenceData.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529


Please change your publicly displayed name so that it conforms with the JavaRanch Naming Policy. You can change it here.
Thanks for your cooperation.

Junilu - [How to Ask Questions] [How to Answer Questions]
Elie Hirschman
Ranch Hand

Joined: Dec 15, 2005
Posts: 37

Even though this is a really old post, it came up for me when I was googling for the right way to output errors in a jsp.
There is a "Correction" at the bottom of that post, which clarifies that you can do this the easy way, like so:

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331

thanks for bringing this thread up...

I found the one thing that was keeping my code from working, in this thread

I was using

instead of
return showForm()

This is my code. If anyone's trying this part.

The controller:

In the controller above I check if the username already exists or not and if it exists I use rejectValue() method that rejects the value of the username field and shows the text from the properties file (I've pasted it below) that corresponds to the key 'field.already.exists'.

Then the return showForm() method is used to show the formView again with the validation error.
(The successView and the formView are configured in the bean)

Why did I do this validation check in the Controller and not in the Validator?
-> Because I cudn't find a way to access my DAO from inside the validator.

The Validator:

What does this Validator do?

1)Checks for empty fields...
2)Is the password < 6 characters...

The Dao:

The Dao has the doesUserExists() method...
I think this check code can be done in just a few lines with a session.get(User.class,username)
but that I think would work only if username was the primary key.
In my table structure, the username was just a field with unique constraint and the pk was userid.
So I tried it this way.

Bean Config:

The form view jsp:

The properties file:


Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Put validation codes in Validator, you can DI Daos to Validator.

SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331

I would like to do it that way...
In my controller, I access getBean() as:

But donno how I can access getBean() in the validator without a request attribute in any of the Validator methods.
What can I do?

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You should use DI over dependency lookup.

Please read
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi Kengkaj,

Thanks a lot !!!
I was of the thought that getBean() was the only way!
(how stupid! a few errors i got before while trying without getBean() made me think so)

This is a lot cleaner!!!

I made a few changes:

and its working great

Thanks again,
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Congrats, using dependency injection is the very reason to use Spring Framework .

From now on, you should realize the full power of Spring . DI is really a great concept, dependencies to implementations could be almost eliminated from source codes.
I agree. Here's the link:
subject: Backend validation - a complete example
It's not a secret anymore!