This blog contains the info about Java and J2ee and Ajax and Hibernate

Tuesday, June 2, 2009

validwhen example in struts

While the Struts Validator provides a host of client-side validation capabilities, it also provides a mechanism for implementing automated server-side validation. The server-side validation that we reviewed inside the ActionForm is powerful, but many times there are some simple validations that you want to perform that you do not necessarily need to write Java code to perform. As an example, consider comparing two fields for equality, such as passwords or email addresses. Furthermore, in order for the Struts Validator to work properly, you should not implement your own validate() method, which means that if you have one custom validation field then you are required to validate the entire form yourself.
This is where the Struts Validator "validWhen" validation rule comes into play. The validWhen rule reads: the form field is valid when the "test" variable's value is true. Or specifically looking at the confirmation password from the previous example:
<field property="confirmPassword" depends="validwhen">
<arg0 key="errors.confirmpassword.match"/>
<var> <var-name>test<var-name>
<var-value>(*this* == password)</var-value>
</var></field>

The validWhen rule works by defining a test variable with the test condition in its value. The value is a boolean expression that can contain the following (extracted from the Struts manual):
Single or double quoted string literals
Integer literals, which can be in decimal, octal, or hexadecimal form
null, which matches a literal null or an empty string
Other form fields, referenced by field name
Indexed form fields, referenced by an explicit integer, such as lastName[2]
Indexed form fields, referenced by an implicit integer, such as lastName[]; the implicit integer index will be the same index of the field being tested (for example this can be used to compare string values character by character)
Properties of indexed form fields (either with an explicit or implicit integer index), for example child[].lastName
The literal *this*, which represents the field currently being tested
Another couple notes about validWhen expressions:
They must be enclosed in parenthesis
You can only compare two values when performing an "and" or "or"
If the values can be converted to integer equivalents then they are converted and compared as such
As an example, if we wanted to allow our user to not set a password, then we would want to accept either "null" as a password or require that both the password and confirmation password match. This can be accomplished as follows:

<field property="confirmPassword" depends="validwhen">
<arg0 key="errors.confirmpassword.match"/>
<var> <var-name>test<var-name>
<var-value>((password == null ) or *this* == password)</var-value>
</var></field>


In this example, if the form's password field is null, the condition is true, or if the confirmation password (this) is equal to the password, the condition is true. Following through this logic, if password is null and confirmPassword is "ABC," the first condition is true and therefore the second condition is ignored. On the other hand, if password is "ABC" and confirmPassword is "DEF," the first condition fails (password is not null). Therefore, the second condition determines the final result; namely, is the confirmation password equal to the password, which it is not.
Writing validWhen expressions becomes easier and easier as you write them, but one of the challenges in using the validWhen is that validation errors do not appear in a JavaScript popup dialog, but rather appears in your tag. Because the tag displays all fields that do not have valid values, the presentation does not work with the tag in its default configuration. In its default configuration it identifies all fields that are not valid before the user completes the form (meaning that everything is invalid!). Therefore, you need to use a variation of the tag that displays an error message only if the specified field is invalid


Note: All the above stuff is extracted from the http://www.informit.com/guides/content.aspx?g=java&seqNum=286.

Cheers,




Monday, June 1, 2009

check box problem in struts

PROBLEM IS:At http://jakarta.apache.org/struts/newbie.html there is a question "Why are my checkboxes not being set from ON to OFF?"
The answer states: "If the value of the checkbox is being persisted, either in a session bean or in the model, a checked box can never unchecked by a HTML form -- because the form can never send a signal to uncheck the box. The application must somehow ascertain that since the element was not sent that the corresponding value is unchecked."
It also states to possibly use a radio button. I am in the process of trying to make this work because I am keeping my form in the session. I am going to try to see if I can check for the element being sent.

ANSWER:

There is a very simple solution to the checkbox problem. The trick is to always add a hidden field AFTER the checkbox with the same parameter name but with the value set to "false":

Basically, the hidden parameter ensures that there is some request parameter submitted, which will always set the corresponding ActionForm property. If the checkbox is checked, the 2 parameters will be passed: "booleanProperty=true&booleanProperty=false". But the beanutils code only uses the first parameter to set the value. If the checkbox is left unchecked, then there will be only 1 parameter "booleanProperty=false" which will ensure the property is set.
The HTML spec says that form elements must be submitted in the GET or POST in the order that they are specified in the HTML, and all browsers I have used obey this rule. There is no risk that the hidden parameter will obscure the form field as long as you always put it after the checkbox.
The beauty of this solution is that it places the burden in the hands of the page author where there can be more flexibility. It also allows the page author to invert the meaning of the checkbox without bothering the application developer. All the page author needs to do is reword the caption, and switch the ordering of the values used in the checkbox and hidden fields. Also, it works with string or numeric properties just as easily.

Another possible solution is :
It is just a work around. This solution will fail when you want to call javascript function on clicking of the checkbox. The Another solution is :- override the "reset" method in the form bean and set the checkbox value false to correctly identify the checkbox value from request. If your bean is in session scope and if you don't want to identify the correct value everytime then get the action from request object and set/reset the checkbox value.