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="sucess">success.jsp</result>
            <result name="input">loginjsp.jsp</result>
        </action>
        <action name="loginpage">
            <result>loginjsp.jsp</result>
        </action>
    </package>
</struts>

In the above struts.xml file,
<constant name="struts.custom.i18n.resources" value="global" />

This line of code specifies that struts2 will use the global.properties file as message resource file. Struts2 will upload all the values of this property file to the value stack and will use them wherever is required.

Now if you want to create different resource file for different language support, you have to just append the suffix "_" and symbol of the language. For example if we want to create the another resource file contain the messages in Indian language (Hindi) we can give the file name as global_in.properties.


To tell the struts that which resource file to be used throughout the user's session, we have to send the query string with any request request_locale=in, then struts2 automatically loads the messages from global_in.properties file.

Now to add the Hindi Unicode characters to the resource file, you have to change the file encoding to utf-8. To do this, used the jdk(in bin folder of the jdk) utility as following.

Here you will get the file global_in1.properties having the encoding utf-8. Then, I deleted the previous ile global_in.properties file and renamed the global1_in.properties file as global_in.properties to use.

 Then you write the following Hindi Unicode messages to the file global_in.properties file to be used.

#Global hindi messages
username =\u092F\u0942\u091C\u0930 \u0928\u093E\u092E
emailid =\u092A\u093E\u0938\u0935\u0930\u094D\u0921
loginpage.title=\u092F\u0942\u091C\u0930 \u0932\u094B\u0917\u0940\u0928
loginpage.heading=\u0915\u0943\u092A\u092F\u093E \u0905\u092A\u0928\u0947 \u0916\u093E\u0924\u0947 \u092E\u0947\u0902 \u0932\u0949\u0917 \u0911\u0928 \u0915\u0930\u0928\u0947 \u0915\u0947 \u0932\u093F\u090F \u0928\u0940\u091A\u0947 \u0915\u0947 \u092B\u093E\u0930\u094D\u092E \u0915\u093E \u0909\u092A\u092F\u094B\u0917 \u0915\u0930\u0947\u0902


Now I am using the above messages in the following jsp page(loginjsp.jsp) to define the labels. In this file, change the charset=utf-8 as specified in the first line.

<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
    <s:text name="loginpage.title" >
       Title Message should be here.
    </s:text>
</title>
</head>
<body>
<h2><s:text name="loginpage.heading">Header</s:text></h2>
<s:form action="myaction" method="post">
  <s:textfield key="username" name="username"/>
  <s:textfield key="emailid"/>
  <s:submit value="submit"/>
</s:form>
</body>
</html>

To access the different resource file, we have to call them as follows.
  • English : http://localhost:8080/ResourceHandling/loginpage
  • Hindi : http://localhost:8080/ResourceHandling/loginpage?request_locale=in
Here is the output screens for the above execution. 






Download complete source code.

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.setDirectoryForTemplateLoading(
                new File("templates"));
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        Template temp = cfg.getTemplate("FMTest2.ftl");
        Map root = new HashMap();
    
        Map order = new HashMap();
        order.put("topic""Purchage Order");
        
        ArrayList items=new ArrayList();
        items.add(new HashMap(){{put("cdrom","23000");put("mouse","2230");put("keybord","2230");}});
        items.add(new HashMap(){{put("cdrom","23000");put("mouse","225");put("keybord","530");}});
        items.add(new HashMap(){{put("cdrom","23000");put("mouse","7650");put("keybord","2230");}});
        order.put("items", items);
    
        root.put("order", order);
        /* Merge data-model with template */
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
        out.flush();
        
    }
}

template file : templates/FMTest2.ftl

Computer Model ${order.topic}   
<#list order.items as i>
  ${i.cdrom}, ${i.mouse}, ${i.keybord}
</#list> 

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 taglib in head tag of the html page.


Here is the source code of the example that is using the struts validation on both sides.

login.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<s:head/>
</head>
<body>
<s:form action="myaction" method="post" validate="true">
  <s:textfield name="username" label="user-name"/>
  <s:textfield name="emailid" label="email-id"/>
  <s:submit value="submit"/>
</s:form>

</body>
</html>



Sucess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2><s:property value="username"/></h2>

</body>
</html>



MyAction.java

import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport {
    
    private String username;
    private String  emailid;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getEmailid() {
        return emailid;
    }
    public void setEmailid(String emailid) {
        this.emailid = emailid;
    }
    
    public String execute()
    {
        
        return "sucess";
    }
}

MyAction-validation.xml


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE validators PUBLIC 
  "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
  "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>

   <field name="username">
        <field-validator type="requiredstring">
            
            <message>Name is required.</message>
        </field-validator>
    </field>
   <field name="emailid">
        <field-validator type="requiredstring">
            <message>email is required.</message>
        </field-validator>
        
        <field-validator type="email">
            <message> valid email is required.</message>
        </field-validator>

        <field-validator type="fieldexpression">
            <param name="expression">emailid.endsWith('@gmail.com')</param>
            <message>Use only gmail.</message>
        </field-validator>
    </field>
</validators>


struts.xml
<?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>
 <package name="default" namespace="/" extends="struts-default">
 <action name="myaction" class="MyAction">
 <result name="sucess">success.jsp</result>
 <result name="input">loginjsp.jsp</result>
 </action>

 </package>
</struts>

Download Source code

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 of the specified field name from the Object passed to validate method.

Now you can define you validation criteria and you can use addFieldError method to add the field error if your validation condition get failed.  If field error is added here, struts framework get intimation that the field validation has failed and shows its corresponding message is displayed on web page.


import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class MyValidator extends FieldValidatorSupport {
    private String domainName;

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }
    public void validate(Object object) throws ValidationException {
        String fieldName = getFieldName();
        Object value = this.getFieldValue(fieldName, object);
        String s = (String) value;
        System.out.println("custom validator invoked for value : "+s);
        if (!s.endsWith(domainName)) {
            addFieldError(fieldName, object);
        }
    }
}

Step-2:
Create file validators.xml in class path where ActionSupport class is and add the following lines there.
<?xml version="1.0" encoding="UTF-8"?> 
 <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
    <validator name="myvalidator" class="MyValidator" />
</validators>

Here myvalidator is the name of validator through which will be refered
MyValidator is the name of the class that is implementing the validation logic.


Step-3 :
Add the following lines of code to use the validator to validate the field .

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC 
  "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
  "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <param name="trim">false</param>
            <message>Username is required.</message>
        </field-validator>
    </field>

    <field name="email">
        <field-validator type="email">
            <message>Email is invalid.</message>
        </field-validator>
        <field-validator type="requiredstring">
            <message>Email is required.</message>
        </field-validator>
        <field-validator type="myvalidator">
            <param name="domainName">gmail.com</param>
            <message>Email must ends with ${domainName}</message>
        </field-validator>
    </field>
</validators>


Here, you are using the validator myvalidator by referring it the type attribute of field-validator element.
Value of the param having the name domainName will be sent to the domainName property of the validator support class MyValidator.

Download Complete source code of this example.

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";
       super.setValue(Integer.parseInt(text));
    }

    @Override
    public String getAsText() {
        System.out.println("getAsTest");
        return ((Number)this.getValue()).toString();
    }    
}


You have to register it in initBinder method in controller for the required type using registerCustomEditor method of binder object.

    public void initBinder(WebDataBinder binder) { 
        logger.info("initBinder invoked for :"+binder.getObjectName());
        binder.registerCustomEditor(int.classnew MyNumberPropertyEditor());
    }

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 and properties of model object are populated with Form's Fields by struts framework. We need to follow the naming convention of HTML form fields and properties of model object to mapping the values properly.

package model;

public class Contact {
    String name;
    String email;
    long phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Contact [name=" + name + ", email=" + email + ", phone="
                + phone + "]";
    }

}
Class Contact is the simple model/entity class having the getter/setters for all properties. You can annotate this class as required for processing by JPA/Hibernate framework.

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class MyAction extends ActionSupport implements ModelDriven {

    model.Contact c = new model.Contact();

    public Object getModel() {
        System.out.println("getModel");
        return c;
    }

    public String execute() {
        System.out.println(c);
        return "success";
    }

}


In action class we are implementing ModelDriven interface and providing implementation of getModel that returns Contact model c. When user submits the form, all values from text fields are mapped to Contact object's properties and execute method print them. Bellow is the HTML page having three text fields. You can notice that name of the text fields and name of the properties of Contact class are same.
<html>
<body>
<h1>Test A file</h1>
<form action="processForm" method="post">
User Name: <input type="text" name="name"/><br/>
Email: <input type="text" name="email"/><br/>
Phone: <input type="text" name="phone"/><br/>
<input type="submit"/> 
</form>
</body>
</html>
Here is the struts.xml file that defins processForm action.
<?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>
    <package name="default" namespace="/" extends="struts-default">
        <action name="processForm" class="MyAction">
            <result name="success">b.jsp</result>
            <result name="input">a.jsp</result>
        </action>
    </package>
</struts>

web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  
  <filter>
    <filter-name>f</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>f</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>



Download Example


Popular Posts