aspose file tools*
The moose likes Struts and the fly likes which is best, stuts validator or javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "which is best, stuts validator or javascript" Watch "which is best, stuts validator or javascript" New topic
Author

which is best, stuts validator or javascript

Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
Hi all,

I have been using struts1.1 for couple of months, but i haven't yet started to use the struts validator. The javascript code in my application is very huge. I just wonder whether i should spend time to shift to struts validator or continue with javascript. Does the struts validator is good enough to replace all the code written in javascript.
Please suggest me.

Thanks


"Failure is not when you fall down; its only when you fail to get up again."
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Choose validate in server side or client side (JavaScript)

1. JavaScript : it's good point to validate data before send this data/request to server. (It's run at client side, so don't have overhead)

2. Server Side validation : it's best point to validate data before execute some process/business method so it's very important. (It's run at server side, so have overhead more than client side script but it's make sure data correctly)



SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Nick Knickerbocker
Greenhorn

Joined: Jan 14, 2002
Posts: 3
Does the struts validator is good enough to replace all the code written in javascript.


Yes, it's good enough. You would be writing your validation code in straight Java rather than javascript. Struts has some tags for easy display of error messages, and it uses properties files for message resources so you can get away from hard coding the error messages.

Something to consider is that your javascript validation, I would assume, is occuring on the client side. If you use struts, the validation occurs server-side, which of course requires a post. If your app is running on just a LAN, like on a corporate intranet, perhaps the performance difference isn't enough to matter. If it's running on the Internet and it's important to you or your customer to give a very quick response on validation (as opposed to "they screwed up, they can wait," which is also okay), then you may want to at least keep javascript validation for the most common errors.

One course of action is to put the validation in both places. That way your users still get an instant response, but if somebody hacks a post to your page or goes through an API or Web service rather than going through the UI, you can still validate the data coming in. That may be overkill for your purposes though.

Have fun!
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
Hi

Thanks for the good points, I might be a little wrong in putting forward my question, i mean to ask regarding the struts validator for javascripting, ie. which we use with validation-rules.xml file. I am very well using the action forms validate method to do the server side scripting.

I assume that when we use the this validator with the html:javascript tag, the javascript code is embedded into the jsp while conversion. is it not?

If so, right now my javascript code uses some DOM methods etc etc and a lot of conditional loops, will this all can be replaced with the struts <html:javascript > tags?

I hope this is clear.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Originally posted by Shankar Narayana:
Hi


I assume that when we use the this validator with the html:javascript tag, the javascript code is embedded into the jsp while conversion. is it not?

If so, right now my javascript code uses some DOM methods etc etc and a lot of conditional loops, will this all can be replaced with the struts <html:javascript > tags?

I hope this is clear.



First question : you'r right.

Second question : I thing, you can not add some DOM method to validation framework but you can add some DOM method into javascript at jsp page (don't use <html:javascript>



The validator-rules.xml File
The validator-rules.xml file defines the Validator definitions available for a given application. The validator-rules.xml file acts as a template, defining all of the possible Validators that are available to an application.

Note: this XML file and the one we'll discuss next must be placed in a location where they can be found by the classloader. When using the Validator with a Web application, the correct location is under the WEB-INF directory.

The validator-rules.xml file is governed by validator- rules_1_1.dtd, which can be found and downloaded at jakarta.apache.org/struts/dtds/validator- rules_1_1.dtd. It would be too time-consuming to go into every detail about the XML definition file, so we'll just cover the essentials here.

The most important element within the validator-rules.xml file is contained with the <validator> element, as shown in Example 1.

Example 1. A simple validator-rules.xml File

<form-validation>
<global>
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>

<validator name="minlength"
classname="org.apache.struts.util.StrutsValidator"
method="validateMinLength"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.minlength"/>
</global>
</form-validation>
There is one <validator> element for each validator that an application uses. In Example 1, there are two validators shown; one is the required validator and the other is the minlength validator. There are many attributes supported by the <validator> element. These attributes are necessary so that the framework knows the correct class and method to invoke on the Validator. For example, in the required validator element in Example 1, the method validateRequired() will be called in the org.apache.struts.util.StrutsValidator class. Validators can also depend on one another. This is shown in Example 1 with the minlength Validator. It includes a depends attribute, which indicates that it depends on the required validator. The msg attribute allows you to specify a key from a resource bundle that the framework will use to generate the correct error message. A resource bundle is used to help localize the error messages.

The <validator> element also supports a <javascript> child element that allows you to specify a JavaScript function that can be executed on the client side. In this way, both server-side and client-side validation can be specified in a single location, making maintenance easier.

Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
The Validation framework is used to validate fields, appropriate to form. But some complex javascript script is not appropriate to apply with this framework and the alternative is write your own script.
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
The <validator> element also supports a <javascript> child element that allows you to specify a JavaScript function that can be executed on the client side. In this way, both server-side and client-side validation can be specified in a single location, making maintenance easier.


By this i understand the I can use all my existing javascript methods with the validator. For simple validations i can go on for defining the rules and for complex things i can just point to a function. Am i right!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which is best, stuts validator or javascript
 
Similar Threads
Validator Framework and Javascript
Have any problem using validator
How to use javascript in Struts
Struts Validation Framework
struts validator