Struts 2 text tag example

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>

<h2>1.<s:text name="name.msg" /></h2> 
Output : <s:text name="name.msg" />

<h2>2. <s:text name="name.msg.unknow">message doesn't exists</s:text></h2> 
Output : <s:text name="name.msg.unknow">message doesn't exists</s:text>

<h2>3. <s:text name="name.msg.unknow" /></h2> 
Output : <s:text name="name.msg.unknow" />

<h2>4. <s:text name="name.msg.param" ><s:param >mkyong</s:param>
</s:text></h2> 
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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
drachidVortilionMayuri WankhadeStruts 2 Tutorials Recent comment authors
newest oldest most voted
drachid
Guest
drachid

you need to add to struts.xml the name of the bundle i.e:
il dosen’t work must indicate

drachid
Guest
drachid

il dosen’t work must indicate

Vortilion
Guest
Vortilion

Hi, what about localisation? How does this work, if I need more than one properties-File?

Mayuri Wankhade
Guest
Mayuri Wankhade

Very useful post ..
var serviceName = document.forms[0].serviceName.value;
var confirmation= confirm(‘‘);

Whrere property file contain :
message.swiftnetservice.recreate=A deleted SWIFTNet Service with name {0} already exists. Do you want to re-create it?

trackback
Struts 2 Tutorials

[…] text tag example Struts 2 text tag is used to get the message from the resource bundle that’s bundle with the action class. […]