Struts 2 i18n and text data tags

<s:i18n>:

The <s:i18n> tag is used to get the message from the resource bundle which is associate to an action or from any other resource.

Syntax:

 <s:i18n name="resourceUrl"> 

Note: In case of resource bundle associate with the action, you have to create the properties file in the same package and having the same name as action class name with .properties extension. 

<s:text>:

The <s:text> tag is used to get the message from the resource bundle and render it.

Syntax:

<s:text name="msgName" /> 

Note: If specified message is not find in the resource bundle then body of the tag will be used as the message and if body is not used or in case of empty body tag name will be used as the message.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>Struts 2 s:i18n and s:text data tag example</title>
	<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </body>
</html>

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 s:i18n and s:text data tag example</title>
	</head>
	<body>
		<h3>This is a s:i18n and s:text data tag example.</h3>
 
		<h4>Message from Test.properties</h4> 
 
		<s:text name="message.show" />
 
		<h4>Message from CustomResource.properties</h4> 
		<s:i18n name="com/w3spoint/action/CustomResource">
			<s:text name="message.show" />
		</s:i18n>
 
		<h4>
                 Message which is not exist in CustomResource.properties
                </h4> 
		<s:i18n name="com/w3spoint/action/CustomResource">
			<s:text name="message.display" />
		</s:i18n>
 
	</body>
</html>

web.xml

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

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.w3spoint.action.Test">
			<result name="success">/test.jsp</result>
		</action>
	</package>
 
</struts>

Test.properties

message.show = This message is from Test.proerties file.

CustomResource.properties

message.show = This message is from CustomResource.proerties file.

Test.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author w3spoint
 */
public class Test extends ActionSupport{
	private String website;
 
	public String execute(){
		return SUCCESS;
	}
 
	public String getWebsite() {
		return website;
	}
 
	public void setWebsite(String website) {
		this.website = website;
	}
 
}

Output:

struts 56   Download this example.   Next Topic: Struts 2 s:url and s:a data tags with example. Previous Topic: Struts 2 set data tag with example.

 

Please follow and like us:
Content Protection by DMCA.com