Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Apache Shale Takes JavaServer Faces to the Next Level : Page 3

Apache Shale, the newest sibling of the Struts framework, leverages JavaServer Faces to enable componentized presentation-tier development. Get started with Shale's Dialog Manager, Validations, and JNDI services.


advertisement

Keep Users in Line with Validations

While you're making life easier for users with wizards, you should keep them in line on the client and server sides as well! Enter Shale Validations.

With Struts, you relegate server-side validations by overriding validate(ActionMapping, HttpServletRequest) in ActionForm or declaring validations for ActionForm fields in the XML configuration file. Client-side validations are accomplished half-heartedly using DynaValidatorForm, as ensuring consistent validation at both the client and the server is difficult, if not impossible, in Struts.

Support for Validations got only slightly better with JSF. JSF provides a pluggable validation framework via its javax.faces.validator.Validator interface, <f:validator/> tag, and faces-config.xml configuration file settings. It provides three specific validations, namely DoubleRangeValidator, LongRangeValidator, and LengthValidator. Although a good start, these are not even close to the types of validations you might need in real world applications.



JSF also seems to have completely overlooked client-side validations. While server-side validations are an absolute must, discerningly used client-side validations can improve your application performance by cutting down on server round trips, and they can even improve user experience.

The good thing about JSF is that it allows you to add your own validations. Shale exploits this extensibility and—leveraging the Apache Commons Validator framework—provides the following additional server- and client-side validation components:

  • Credit Card
  • Date
  • Email
  • Generic
  • ISBN
  • URL

This is just a starting lineup, expect to see a lot more validations as Shale matures.

The Launch Party application uses several Shale validations in userid.jsp and creditcard.jsp. Listing 7 shows the validation of email in userid.jsp. All the other validations follow a similar pattern:

... <%@include file="messages.jspf"%> ... <h:form id="userid" onsubmit="return validateForm(this);"> <h:outputText value="*** DEBUG MODE ***" rendered="#{jndi.debugMode}"/> ... <h:outputText value="* Email Address:"/> <h:inputText id="email" value="#{registrationBean.email}"> <s:commonsValidator type="required" message="#{launchPartyMessages.EMAIL_REQUIRED}"
client="true" server="true"/> <s:commonsValidator type="email" message="#{launchPartyMessages.EMAIL_INVALID}"
client="true" server="true"/> </h:inputText> <h:message for="email" styleClass="errors"/> ... <s:validatorScript functionName="validateForm"/> ... Listing 7. Email Validation in userid.jsp

The email address field on userid.jsp is required and it must be of valid email address format. Listing 7 uses two <s:commonsValidator/> tags, one with the type 'required' and other with the type 'email'. Both client- and server-side validations are enabled.

The client validations require <s:validatorScript/>, which generates the JavaScript function for validation at run time in the JSP page, and the onsubmit function in the h:form tag is set to use the validation function created by <s:validatorScript/>.

The messages displayed for user infractions are pulled in from the LaunchPartyMesssages.properties file via a clever include of messages.jspf at the top of userid.jsp.

LaunchPartyMessages.properties is a run-of-the-mill resource bundle that contains a key/value pair of error messages (as shown in Listing 8). This file is in the com/techyatra/shale folder, which is also in the Java package folder for the Launch Party application.

# Launch Party ResourceBundle properties file ... EMAIL_REQUIRED=Email address is required EMAIL_INVALID=Email address is invalid ... Listing 8. LaunchPartyMessages.properties File in com/techyatra/shale

Messages.jspf contains declaration of variable launchPartyMessages using JSF's <f:loadBundle/> tag as shown in Listing 9 below.

... <f:loadBundle var="launchPartyMessages" basename="com.techyatra.shale.LaunchPartyMessages"/> ... Listing 9. Messages.jspf

Shale Validation code is externalized in org/apache/shale/validator/validator-rules.xml, which you'll find in the shale-core.jar file. Have a look at it. It's quite a riveting read!



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap