Skip to main content

Posts

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.Default...

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 strut...

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 va...

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   {     ...

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...

Spring Framework : ApplicationContext instantiation for web applications in Spring Framework

In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader. ContextLoaderListener : It is listener implementation that is added to web.xml file. ContextLoaderServlet : It is servlet implementation that is configured with load-on-startup tag in web.xml.   ContextLoaderListener is simple way to use the spring in web application. this listener accept contextConfigLocation parameter from context-parama. You have to enter following code in web.xml file define contextConfigLocation parameter in context-param tag. <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>/WEB-INF/services.xml</param-value> </context-param>   services.xml is the Spring configuration file in which you define beans. Add the ContextLoaderListener listener. <listener>   <listener-class>org.springframework.web.context.ContextLoaderListener</li...