Skip to main content

Posts

Showing posts from September, 2012

FreeMarker : Example

Freemarker is a simple templating engine that can generate text contents from any type of template. It is so simple that you can learn it in a single day and can use it in any project. It helps you when you need to produce bulk text contents of the common format like newsletters, green mail, notifications etc. package  fm; import  java.io. File ; import  java.io. IOException ; import  java.io. OutputStreamWriter ; import  java.io. Writer ; import  java.util. ArrayList ; import  java.util. HashMap ; import  java.util. Map ; import  com.sun.tools.javac.util. List ; import  freemarker.template. Configuration ; import  freemarker.template.DefaultObjectWrapper; import  freemarker.template.Template; import  freemarker.template.TemplateException; public   class  FMTest2  {      public   static   void   main ( String []  args )   throws   IOException , TemplateException  {          Configuration  cfg  =   new   Configuration () ;         cfg. setDirectoryForTemplate

Struts2 - Enabling Client Side validation

Struts2 provides the declarative option to define the validation rules for the data fields in form. You can use the default implementation of validation framework of the struts2 to validate various kind of data. When you use the validation feature of the struts2, it is done server side by default. But setting a single attribute of the form tag, it can be applied at client side. Struts2 validation process can work on server side as well as client side. When it works at server side, server return input form containing validation error along with  the text fields. when you use client side validation feature, the javascript will process the validate without sending the form data to server for validation. it means data is validated before going to server. this javascript is generated by struts frame work into the rendered webpage having the form. this validation works when you set the validation attribute to true in the form tag. And you have the add the head tag of the struts ta

Struts2 - Writing custom validator

Sometimes it is needed that the existing validation rules those are implemented by struts framework do not satisfy project validation criteria. That you have to write your logic according to your requirements. Struts2 provides the facility to write your own validation class in which you can implement your own validation logic. Step-1: Write down a class that extends inbuilt validator support class. In this example I am extending the FieldValidatorSupport class that the validate () method that has to be overridden by your custom validator class. Here we can create the variable (i.e. property) e.g. domainName (or other) and its getter-setter in which the values will be passed from validation (xml) file as parameters. Method validate () is overridden to implement your validation logic. This method takes the Object to be validated at run time that is passed by interceptor. The method  getFieldValue () which is inherited from super class will help you to retrieve the actual value