Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where to put validation code???

 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hung Tang
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 314
2
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic