Skip to main content

Posts

Showing posts from 2012

Struts2 Resource Handling - Using Hindi language in internationalization (i18n)

Struts2 provides us different ways for handling message resources. Struts2 supports internationalization for different locales and we can provide message resources for different locales. Struts2 will use resource file naming convention through which it identifies that which message resource file to be used within the particular session. See the following configuration. <? xml   version = "1.0"   encoding = "UTF-8" ?> <! DOCTYPE   struts   PUB      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      "http://struts.apache.org/dtds/struts-2.0.dtd" > < struts >      < constant   name = "struts.custom.i18n.resources"   value = "global"   / >      < package   name = "default"   namespace = "/"   extends = "struts-default" >          < action   name = "myaction"   class = "MyAction" >              < result   name

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

Spring MVC - Avoid "nested exception is java.lang.IllegalArgumentException" while mapping blank text field to int type using Spring MVC. Using custom editor to process numeric fields values.

In Spring MVC, while I was capturing numeric value in int type property from the form, I was getting exception if the field is left blank instead of putting 0. If the numeric field is optional in form, I have to put default zero value for the numeric field (like phone number field) to pass the validation of that form. To avoid this situation, we can register our Custom editor for particular type to process the value for desired result. Here is the MyNumberPropertyEditor class that process the Number type values. It sets value 0 to blank property so that validator can get 0 value while validating property. package  utils; import  java.beans. PropertyEditorSupport ; public   class  MyNumberPropertyEditor  extends   PropertyEditorSupport   {     @Override      public   void   setAsText ( String  text )   throws   IllegalArgumentException   {          System .out. println ( "setAsTest" ) ;          if ( text. length () == 0 ) text = "0" ;        

Struts 2 | ModelDriven to handle Form Data

In struts2, values of request parameters(HTML form's fields) are mapped to matching properties of the action class . Struts populates the values come in Http Request to the variables having same name of input fields of the HTML form. When we want to persist the data received form request, it needs to be in model object that can be persisted by ORM framework like JPA or Hibernate. So if we are using the Action class to handle the values from form, we have to mark it as Entity for JPA/Hibernate to persist its data. Therefore using the action class as Entity class is not a best practice. Struts2 provides another option to handle to form data. We can use separate Entity bean/Object to receive the form data rather than Action class. In terms of struts, it is called Model object. This is the same thing as Entity object in terms of JPA/Hibernate. In struts, we have to implement the ModelDriven interface that has the getModel method. This method returns the model object an