aspose file tools*
The moose likes Struts and the fly likes where to put validation code??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "where to put validation code???" Watch "where to put validation code???" New topic
Author

where to put validation code???

Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
I have a doubt regarding when to develop user's data in a web based application. when i say validation i do not mean any validation that requires database access, i mean normal validation i.e. whether or not all the mandatory fields are entered, if they are entered do they contain valid data(i.e. age should be non-negative) etc.
Of course javascript validation will be there but there is no guarantee that user has enabled it. so it is necessary that we should validate it before passing it to database. Now i have two options(I am using struts architecture) one in web-tier another in my middle-tier(EJB).
one book on struts (Struts in Action) says that is should do it in web-tier, good enough as that will remove burder from next layers, but my challenge is that we have to develop a middle tier which shoulod be totally(or as much as possible) independent from front end, so that it can be reused(possibly with different fronyt end). So i think i should put my validation code on middle tier. Putting validation on both tier can slow down the application un necssarily.
what should i do??
what is general practice ?
plz help


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148
Since you are using Struts, it's possible (as described in SIA) to use both the javascript validation (at the web layer) and the form validation (at the server layer) at the same time. So if the web client turns off javascript, the Struts framework can still perform validation at the expense of the server.
I suggest you should create a Form-To-DTO mapping in your actions so you decouple the web layer from your model layer. Your model layer would accept these DTOs and use them to persist to the DB or whatever you want to do with them.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Originally posted by Hung Tang:

I suggest you should create a Form-To-DTO mapping in your actions so you decouple the web layer from your model layer. Your model layer would accept these DTOs and use them to persist to the DB or whatever you want to do with them.

thanx for the reply Tang,
can u plz explain more about this technique, or alternatively provide link for a tutorial on this.
I am new to struts so i am not aware of all its capabilities.
thanx again
Darryl A. J. Staflund
Ranch Hand

Joined: Oct 06, 2002
Posts: 303
Hi all,
Manning Press has made Chapter 12 of their book:
Husted, Ted et al. Struts in Action: Building Web Applications with the Leading Java framework (Greenwich: Manning Press, 2003)
available for download in PDF format from the following URL:
http://www.manning.com/husted/chap12.pdf
This chapter discusses the Validator framework that can be used to validate data entry forms, etc. In particular, it covers the following topics:
- Understanding the need to validate data.
- Configuring and using the Commons Validator.
- Using multiple and localized validations.
- Writing your own validators.
As the Validator framework is fairly new and does not come with a lot of documentation, this chapter is well worth the read.
Darryl
Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148

can u plz explain more about this technique, or alternatively provide link for a tutorial on this.

Here you go:
http://www.mycgiserver.com/~andrej/technical/struts/struts.jsp
 
wood burning stoves
 
subject: where to put validation code???