Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
09 Struts Automatic Validation

09 Struts Automatic Validation

Ratings: (0)|Views: 22|Likes:
Published by api-26793394

More info:

Published by: api-26793394 on Oct 17, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Slides \u00a9 Marty Hall, http://www.coreservlets.com, books \u00a9 Sun Microsystems Press

Jakarta Struts:
Automatically
Validating Input

Struts 1.2 Version
CoreServlets & JSP book: www.coreservlets.com
MoreServlets & JSP book: www.moreservlets.com
Servlet/JSP/Struts/JSF Training: courses.coreservlets.com
Slides \u00a9 Marty Hall, http://www.coreservlets.com, books \u00a9 Sun Microsystems Press
For live Struts training, please see
JSP/servlet/Struts/JSF training courses at
http://courses.coreservlets.com/.
Taught by the author ofCore Servlets and JSP,Mor e
Servlets and JSP, and this tutorial. Available at public
venues, or customized versions can be held on-site at
yourorganization.
Apache Struts: Validating User Input Automatically
5
www.coreservlets.com
Overview
\u2022Distinguishing manual validation from
automatic validation
\u2022Distinguishing client-side validation from
server-side validation
\u2022Using automatic validation

\u2013Declare application-wide properties file
\u2013Add messages to properties file
\u2013Turn on the automatic validator
\u2013Put validation rules in validation.xml
\u2013Put <html:errors/> in input page
\u2013Enable JavaScript validation

Apache Struts: Validating User Input Automatically
6
www.coreservlets.com
Options for Form Field
Validation
\u2022Do validation in the Action

\u2013Most powerful; has access to business logic, DB, etc.
\u2013May require repetition in multiple Actions
\u2013Must manually map conditions back to input page
\u2013Must write validation rules yourself

\u2022Do validation in the form bean
\u2013In individual setter methods
\u2022Not really validation, but can be used to modify values
\u2013Using the validate method

\u2022Not quite as powerful
\u2022Does not require repetition in multiple Actions
\u2022Will automatically redisplay input page
\u2022Still requires you to write validation rules yourself

\u2022Use automatic validator
\u2013Handles many common cases; includes JavaScript
\u2013You can combine approaches in the same application
Apache Struts: Validating User Input Automatically
7
www.coreservlets.com
Manual Validation
(See Previous Section for Details and Examples)
\u2022Option 1: Put validation code in the Action

\u2013Return custom conditions from Action
\u2013Map certain conditions back to the input form
\u2013Embed the messages in the form bean

\u2022Option 2: Put validation code in bean
\u2013Insert <html:errors/> in input form
\u2013Use validate method in ActionForm class
public ActionErrors validate(ActionMapping mapping,

HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (somethingWrongWith(someField)){

errors.add("someField",
new ActionMessage("errors.someField"));
}return errors;
}
Apache Struts: Validating User Input Automatically
8
www.coreservlets.com
Manual vs. Automatic
Validation
\u2022Manual validation

\u2013Most flexible
\u2013Has full access to bean and to business logic and database
\u2013Repeats same logic many times
\u2013Runs only on server if you use existing framework

\u2022Client-side validation requires writing lots of JavaScript
\u2013Tedious
\u2013Embedded in Java code
\u2022Which violates Struts strategy of having as much as possible in
editable XML files
\u2022Automatic validation

\u2013Consolidates validation code
\u2013Lets you use standard validation rules
\u2013Runs on server; can optionally also run on client
\u2013Described by XML files

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->