12:16 pm - Sunday June 25, 2017

Struts 2 i18n Localization Example and Tutorial

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Struts 2 i18n Localization Example and Tutorial

Screenshot-3

Screenshot

Localization.java


package com.candidjava.controller;

import com.opensymphony.xwork2.ActionSupport;

public class Localization extends ActionSupport{
private String name;
private int age;

public String execute()
{
return SUCCESS;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

global.properties


global.name = Name
global.age = Age
global.submit = Submit
global.heading = Select Locale
global.success = Successfully authenticated

global_es.properties


global.name = Nombre de usuario
global.age = Edad
global.submit = Presentar
global.heading = seleccionar la configuracion regional
global.success = Autenticado correctamente

struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="global" />

<package name="helloworld" extends="struts-default" namespace="/">
<action name="empinfo"

method="execute">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>

<action name="locale"

method="execute">
<result name="success">/index.jsp</result>
</action>
</package>

</struts>

web.xml


<?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_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

index.jsp


<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>

<h1><s:text name="global.heading"/></h1>

<s:url id="indexEN" namespace="/" action="locale" >
<s:param name="request_locale" >en</s:param>
</s:url>
<s:url id="indexES" namespace="/" action="locale" >
<s:param name="request_locale" >es</s:param>
</s:url>
<s:url id="indexFR" namespace="/" action="locale" >
<s:param name="request_locale" >fr</s:param>
</s:url>

<s:a href="%{indexEN}" >English</s:a>
<s:a href="%{indexES}" >Spanish</s:a>
<s:a href="%{indexFR}" >France</s:a>

<s:form action="empinfo" method="post" namespace="/">
<s:textfield name="name" key="global.name" size="20" />
<s:textfield name="age" key="global.age" size="20" />
<s:submit name="submit" key="global.submit" />
</s:form>

</body>
</html>

success.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>
<title>Success</title>
</head>
<body>
<s:property value="getText('global.success')" />
</body>
</html>

Output Screenshots

Screenshot-1Screenshot-2Screenshot-4

 

 

 

 

 

 

 

 

 Download Struts2_Localization

BACK

 

 

Filed in: Struts 2 Tutorial

Comments are closed.