Проснулась от маминых слов "Саша, уже пол восьмого." С начала никак не могла понять кто это говорит. Причем у меня за спиной а не с порога комнаты, как это обычно случается. До меня тихонько доходит что разговаривает со мной мама и я наконец вспоминаю что мне было влом убирать все со своей кровати и было холодно, так что я забралась спать с мамой.
Так странно. Все время, сколько я себя помню, родители всегда хлопотали о нашем благополучии, вечно работали, где-то бетали, все покупали, кудато возили, что-то готовили. Мама всегда была дома с нами после школы, папа приходил тоже не так уж и поздно а все же я никогда не ощущала что они рядом, никогда не была близка с родителями.
Wednesday, March 21, 2007
Thursday, March 08, 2007
Integrating Jakarta Validation with Spring using Spring Modules
This is the first in the never ending series of tutorials I will be posting because I'm getting pissed off at work at how open source technologies lack such useful resources.
NOTE: This tutorial is meant to be an addition to this one.
Before you begin.
At the time of writing of this tutorial the following versions were referred to:
Spring 2.0
Springmodules 0.7
Here are a few resources you may find useful, in addition to those, mentioned in this tutorial.
DefaultValidatorFactory (source code)
DefaultBeanValidator (source code)
AbstractBeanValidator (source code)
ConfigurableBeanValidator (source code)
MultiActionController (javadoc)
BindException (javadoc)
YouTube (music videos to ease the pain)
1. Configuring Validator Factory.
This is a simple and easy step of adding a ValidatorFactory bean to Spring applicationContext.xml. See the aforementioned documentation above for reference.
2. Validation Rules!
This step is also self explanatory since you're simply to download the provided validation-rules.xml and save it at the location you've specified in your ValidatorFactory bean.
3. Configure your Validator.
This is also pretty easy understand from the provided documentation.
4. Client side validation.
I'm going to swap two steps of client and server side validation since I found that order to be more intuitive to those who have never tried anything related to validation in their life.
So, really, what does the client side validation? Yup, you've guessed it, JavaScript that comes with the package. However, adding that javascript to your JSP page isn't as easy.
Import proper tag library.
To do so, simply add a declaration snippet to import the validation tags.
Add javaScript to the page.
To do so, use the newly imported validator tag to import the code:
Enable validation of your form.
If you're using a simple <form> tag, then simply add
Using DefaultBeanValidator.
This class provides automation of resolving your form's name. When using this validator, it is assumed that the name of the form is the same as the class name of the form's backing bean.
In contrast, this class provides flexibility by allowing to configure the form's name it's validating through Spring's bean invocation. Simply create a validator bean for each form and you're off to the races!
Now that we've decided on the naming of the forms, it's important to tell the validator what are the valid conditions that the data will be validated against.
Now this is the part where it gets all confusing. There are a bunch of pre-made validators that are readily defined in validation-rules.xml. A few of the most common ones are
The DTD provides a short overview of the different tags and fields that are available to the user to enable the validation of a particular form. (they are also rather intuitive) However, one particular tag that lacks any explanation is the <var> tag with two of its children, <var-name> and <var-value>. These are used together with such validators as validWhen to provide necessary variables for testing.
5. Server side validation.
Ah, here comes another fun part. If you thought you're done, think again. Now that you've configured your client side to invoke validations, the errors are thrown around, (hopefully) logged somewhere and yet nothing is being done to fix this mess.
The online documentation covers the set up of the controller bean, in case you decide to go with the SimpleFormController implementation. However, in the world of dynamics we venture off to use AJAX in our web applications and thus decide to utilize the functionality of MultiActionController class. Its set up differs just a tad, however, from that of SimpleFormController.
NOTE: This tutorial is meant to be an addition to this one.
Before you begin.
At the time of writing of this tutorial the following versions were referred to:
Spring 2.0
Springmodules 0.7
Here are a few resources you may find useful, in addition to those, mentioned in this tutorial.
DefaultValidatorFactory (source code)
DefaultBeanValidator (source code)
AbstractBeanValidator (source code)
ConfigurableBeanValidator (source code)
MultiActionController (javadoc)
BindException (javadoc)
YouTube (music videos to ease the pain)
1. Configuring Validator Factory.
This is a simple and easy step of adding a ValidatorFactory bean to Spring applicationContext.xml. See the aforementioned documentation above for reference.
2. Validation Rules!
This step is also self explanatory since you're simply to download the provided validation-rules.xml and save it at the location you've specified in your ValidatorFactory bean.
3. Configure your Validator.
This is also pretty easy understand from the provided documentation.
4. Client side validation.
I'm going to swap two steps of client and server side validation since I found that order to be more intuitive to those who have never tried anything related to validation in their life.
So, really, what does the client side validation? Yup, you've guessed it, JavaScript that comes with the package. However, adding that javascript to your JSP page isn't as easy.
Import proper tag library.
To do so, simply add a declaration snippet to import the validation tags.
<%@ tglib uri="http://www.springmodules.org/tags/commons-validator" prefix="validator" %>
Add javaScript to the page.
To do so, use the newly imported validator tag to import the code:
<validator:javascript formName="account" staticJavascript="false" xhtml="true" cdata="false"/>
Enable validation of your form.
If you're using a simple <form> tag, then simply add
onsubmit="return validateMeaningfulNameForm(this)"to your form tag declaration. If you're using Spring's <form:form> tag, I suggest externalizing all the code required for form submission into a function. I used property binding method, thus my function looked like this:
function formSubmit(){
alert("Submitting and validating the form data.");
document.meaningfulNameForm.actionMethod.value="submitForm";
validateMeaningfulNameForm(this);
}Naming of your form is very important. You have two options in this.Using DefaultBeanValidator.
This class provides automation of resolving your form's name. When using this validator, it is assumed that the name of the form is the same as the class name of the form's backing bean.
Example. Imagine I have a class org.mycompany.forms.Data which is meant to represent the data I get submitted through my form. (i.e. my Data class is the backing bean for my form) If you're using DefaultBeanValidator then your form's name MUST be data, due to the way this validator class resolves the form names.Using ConfigurableBeanValidator.
In contrast, this class provides flexibility by allowing to configure the form's name it's validating through Spring's bean invocation. Simply create a validator bean for each form and you're off to the races!
Example. Imagine that I like to name my forms with meaningful names and have defined the form in the following manner:Defining validation rules for your form.<form id="meaningfulNameForm" name="meaningfulNameForm" onsubmit="return validateMeaningfulNameForm">Thus I will create the following bean in my applicationContext.xml:<bean id="meaningfulNameFormValidator" class="org.springmodules.commons.validator.ConfigurableBeanValidator">I will then need to pass this bean to an appropriate controller for my form.
<property name="formName" value="meaningfulNameForm" />
</bean>
Now that we've decided on the naming of the forms, it's important to tell the validator what are the valid conditions that the data will be validated against.
Now this is the part where it gets all confusing. There are a bunch of pre-made validators that are readily defined in validation-rules.xml. A few of the most common ones are
- required - defines a field as a required field and verifies that the value of the field is not null and its length is not 0.
- validWhen - ensures that the expression, specified in the test parameter is true (explained further).
The DTD provides a short overview of the different tags and fields that are available to the user to enable the validation of a particular form. (they are also rather intuitive) However, one particular tag that lacks any explanation is the <var> tag with two of its children, <var-name> and <var-value>. These are used together with such validators as validWhen to provide necessary variables for testing.
Example. When using validWhen validator, you need to provide a boolean expression to be verified. This is done through <var> tag:The tutorial, mentioned at the beginning of the post shows a simple set up. One thing to note is the fact that you have to make sure that you're specifying the proper form name in the <form> tag.<var>This validator is expecting a boolean variable, named test, to be passed in with the value of the boolean expression in order to validate the form field.
<var-name>test</var-name>
<var-value>(*this* == password)</var-value>
</var>
5. Server side validation.
Ah, here comes another fun part. If you thought you're done, think again. Now that you've configured your client side to invoke validations, the errors are thrown around, (hopefully) logged somewhere and yet nothing is being done to fix this mess.
The online documentation covers the set up of the controller bean, in case you decide to go with the SimpleFormController implementation. However, in the world of dynamics we venture off to use AJAX in our web applications and thus decide to utilize the functionality of MultiActionController class. Its set up differs just a tad, however, from that of SimpleFormController.
<bean id="multiActionController" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">The controller will throw a BindException which you may use to access such information as which field was not validated and why. This can be used by some custom controller to react accordingly or to simply display proper error messages to the user.
<property name="validators" >
<list>
<ref bean="beanValidator" /> <!-- this is your validator bean, defined earlier -->
</list>
</property>
</bean>
Friday, March 02, 2007
Moving
Well, there is a lot of movement right now at work. Everyone decided to swap the desks, well except me, the poor co-op, and Leena, one of the business analysts. Well, Shawn gets to stay put right behind me as well, but he's away and can't share the excitement. The local repository is still down and due to the move probably won't be up until lunch. Hopefully by then all the Crispy Creme(TM) doughnuts will be gone - I've eaten two and they're just soooo guuuuud! Now I have to go dancing for the the entire weekend to lose all those calories!
Yesterday we had a huge snowstorm (pictures will be available soon on Picasa), getting home was CRAZY. But somehow, once again, yours truly managed to avoid all the troubles. I admit, the ride on the bus for half of the way standing in a packed bus wasn't my first choice but it was the ONLY choice.
I think I am slowly infecting the office and everyone else around me - my sister got sick last week, people at work got sick, now Dima is sick too. He was whining the whole day how bad he has it, sitting at home, drinking tea and doing nothing. So Anton M. was bored yesterday and we made a surprise visit over to his house. We were watching "Братва и Кольцо" - hilarious Russian voice-over for Lord of the Rings. I have to say, if you have 3-9 hours free and want to spend them having absolute blast, just invite a couple of friends over and watch just that. To all the Russian speaking readers, I've got a full DVD for those who haven't seen it.
The drive there was an adventure! Anton M. has summer tires and doesn't plan to change them. Thus TCS (Traction Control System) was going off quite a number of times otherwise it would be impossible to get up that hill on Bathurst. Drive back was better, the snow on the roads just melted but didn't freeze just yet so the car had just enough grip to get me home ^_^
Fridays are good days. My favorite day of the week is Thursday but because of Fridays. Nobody wants to stay at work or be serious (thank God I have never had a deadline yet at work!). It was sunny at around 10am and even though the sun is gone, the mood stayed light. Even though I still won't get a second monitor. But oh well, can't expect much when you're in my shoes. Посмотрим что они запоют когда им захочется что бы я вернулась на еще один семестр!
Over and out, minna!
Yesterday we had a huge snowstorm (pictures will be available soon on Picasa), getting home was CRAZY. But somehow, once again, yours truly managed to avoid all the troubles. I admit, the ride on the bus for half of the way standing in a packed bus wasn't my first choice but it was the ONLY choice.
I think I am slowly infecting the office and everyone else around me - my sister got sick last week, people at work got sick, now Dima is sick too. He was whining the whole day how bad he has it, sitting at home, drinking tea and doing nothing. So Anton M. was bored yesterday and we made a surprise visit over to his house. We were watching "Братва и Кольцо" - hilarious Russian voice-over for Lord of the Rings. I have to say, if you have 3-9 hours free and want to spend them having absolute blast, just invite a couple of friends over and watch just that. To all the Russian speaking readers, I've got a full DVD for those who haven't seen it.
The drive there was an adventure! Anton M. has summer tires and doesn't plan to change them. Thus TCS (Traction Control System) was going off quite a number of times otherwise it would be impossible to get up that hill on Bathurst. Drive back was better, the snow on the roads just melted but didn't freeze just yet so the car had just enough grip to get me home ^_^
Fridays are good days. My favorite day of the week is Thursday but because of Fridays. Nobody wants to stay at work or be serious (thank God I have never had a deadline yet at work!). It was sunny at around 10am and even though the sun is gone, the mood stayed light. Even though I still won't get a second monitor. But oh well, can't expect much when you're in my shoes. Посмотрим что они запоют когда им захочется что бы я вернулась на еще один семестр!
Over and out, minna!
Subscribe to:
Posts (Atom)