• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Backend validation - a complete example

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy cow! All that just to show some errors on the JSP? Spring MVC at it's finest.
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More info here: http://www.salixalba.uklinux.net/willow/computing/spring.html
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {
bindException.reject....
...
}

Is badUsername being set in the validator or controller?

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


blue
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Mike:
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.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"BLUE COW"

Please change your publicly displayed name so that it conforms with the JavaRanch Naming Policy. You can change it here.
Thanks for your cooperation.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jordi Monn� wrote:More info here: http://www.salixalba.uklinux.net/willow/computing/spring.html



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:


 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

thanks for bringing this thread up...

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

I was using

showForm()
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:


HTH,
Vishwa
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put validation codes in Validator, you can DI Daos to Validator.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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?

Thanks,
Vishwa
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use DI over dependency lookup.

Please read http://static.springframework.org/spring/docs/2.5.x/reference/index.html.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Vishwa
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic