Struts 2 “text” tag is used to get the message from the resource bundle that’s bundle with the action class. And follow the three sequences :

  1. Display the message from resource bundle, follow the Struts 2 resource bundle search order.
  2. If the message is not found in the resource bundle, then the body of the tag will be displayed.
  3. If the body of the tag is empty, then the value of the “name” attribute in the <s:text> tag will be displayed.

This is best illustrated with a complete example :

1. Action

An Action class to forward the request.

TextTagAction.java

package com.mkyong.common.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class TextTagAction extends ActionSupport{
 
	public String execute() throws Exception {
 
		return SUCCESS;
	}
}

2. Propeties file

A simple properties file with two keys “name.msg” and “name.msg.param“.

TextTagAction.properies

name.msg = "This is a message from properties file"
name.msg.param = "This is a message from properties file - param : {0}"

3. text tag example

It shows the use of “text” tag.

text.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 text tag example</h1>
 
<h3>1.&lt;s:text name="name.msg" /&gt;</h3> 
Output : <s:text name="name.msg" />
 
<h3>2. &lt;s:text name="name.msg.unknow"&gt;message doesn't exists&lt;/s:text&gt;</h3> 
Output : <s:text name="name.msg.unknow">message doesn't exists</s:text>
 
<h3>3. &lt;s:text name="name.msg.unknow" /&gt;</h3> 
Output : <s:text name="name.msg.unknow" />
 
<h3>4. &lt;s:text name="name.msg.param" &gt;&lt;s:param &gt;mkyong&lt;/s:param&gt;
&lt;/s:text&gt;</h3> 
Output :
<s:text name="name.msg.param" >
	<s:param >mkyong</s:param>
</s:text>
 
</body>
</html>

How it work?
1. <s:text name=”name.msg” />
Get and display the message from the resource bundle (TextTagAction.properies) that’ associate with the current action class (TextTagAction.action).

 "This is a message from properties file"

2. <s:text name=”name.msg.unknow”>message doesn’t exists</s:text>
The key is not found in the resource bundle “TextTagAction.properies” or any search order, So the body of the tag is displayed.

message doesn't exists

3. <s:text name=”name.msg.unknow” />
No message found in resource bundle and body of the tag, So the value in the “name” attribute is displayed.

name.msg.unknow

4. <s:text name=”name.msg.param” ><s:param >mkyong</s:param></s:text>
Pass a parameter into the resource bundle via a <param> tag.

"This is a message from properties file - param : mkyong"

4. struts.xml

Link it ~

<?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" />
	<package name="default" namespace="/" extends="struts-default">
 
		<action name="textTagAction" 
			class="com.mkyong.common.action.TextTagAction" >
			<result name="success">pages/text.jsp</result>
		</action>
 
	</package>
</struts>

5. Demo

http://localhost:8080/Struts2Example/textTagAction.action

Output

Struts 2 text tag example

Reference

  1. Struts 2 text tag documentation
Note : You can find more similar articles at - Struts 2.x Tutorials