In this article, we will go through the text tag. But before going directly to the text tag, let's first understand struts2. Struts 2 is an MVC-based corporate Java web application development framework. The original Struts framework has been completely rewritten. It offers a wide feature set and an open source API implementation.
The text tag is one of the struts2 data tags, the data tags in Struts 2 are mostly used to manipulate the data presented on a page. Some common data tags are the Action Tag, the Include Tag, the Bean Tag, the Date Tag, the Param Tag, the Property Tag, the Text Tag, and the URL Tag. In this blog, we will be talking particularly about the Text tag.
The Text Tag
The text tag is a generic element used to display an I18n text message. The message must be contained in a resource bundle with the same name as the action with which it is linked. In reality, this means that you should create a properties file with the same name as your Java class, but with the .properties suffix, in the same package as your Java class.
The text tag is a generic element used to display an I18n text message. Choose one of the three options:
The message must be contained in a resource bundle with the same name as the action with which it is linked. In reality, this means that you should create a properties file with the same name as your Java class, but with the .properties suffix, in the same package as your Java class.
If the named message cannot be found, the tag's content will be used as the default message.
If nobody is specified, the message's name will be used.
Implementation
We are writing a programme to understand the Test Tag Implementation. First, we will construct a Ninja Class, then a View, and last, we will configure the file. So let us get started.
Create Ninja Classes
package com.CodngNinjas.struts2;
public class HelloNinja {
private String name;
public String execute() throws Exception {
return "submit";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create Views
Let us start with HelloNinja.jsp with the following content:
<%@ 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>Coding Ninjas </title>
</head>
<body>
<s:i18n name = "HelloNinja">
<s:text name = "name.submit"/><br>
<s:text name = "name.xyz">No message is there</s:text><br>
<s:text name = "name.message.param">
<s:param >Ninjas</s:param>
</s:text>
</s:i18n>
</body>
</html>
Configuration Files
Let us make a property file with the same name as the package name of your action class. In this scenario, we will create a HelloNinja.properties file and place it in the classpath.
name.submit = This is a submit message
name.message.param = The best Coding Website - Coding : {0}
Your struts.xml file should look like this:
<?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 = "ApplicationResources"/>
<package name = "helloninja" extends = "struts-default">
<action name = "hello"
class = "com.CodngNinjas.struts2.HelloNinja"
method = "execute">
<result name = "success">/HelloNinja.jsp</result>
</action>
</package>
</struts>
Right-click on the project name and select Export > WAR File.
After that, place this WAR in Tomcat's web apps directory.
Then launch Tomcat
Navigate to the URL http://localhost:8080/CodingNinjasStruts2/system.action.
Output
This will result in the screen shown below.
Frequently Asked Questions
What exactly is Struts 2?
Struts 2 is an open-source web application framework that may be used to create Java EE web applications. It uses and extends the Java Servlet API to encourage developers to employ a model-view-controller (MVC) architecture.
What are the uses of the Struts 2 framework?
Struts 2 is a framework for creating MVC (Model View Controller)-based web applications. Struts 2 is a hybrid of the opensymphony webwork framework and struts 1.
webwork + struts1 = struts2
Write some Common Data tags.
The Action Tag, The Include Tag, The Bean Tag, The Date Tag, The Param Tag, The Property Tag, The Text Tag, and The URL Tag are common data tags.
What is the Text Tag?
The text tag is a generic element used to display an I18n text message. The message must be contained in a resource bundle with the same name as the action with which it is linked.
How can you produce a WAR file?
To produce a War file, right-click on the project name and select Export > WAR File. After that, place this WAR in Tomcat's web apps directory. Finally, launch Tomcat and navigate to the URL http://localhost:8080/CodingNinjasStruts2/system.action.
Conclusion
In this article, we learned about the text tag, which includes Creating and Viewing the text tag.
After reading about the text tag, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see the Custom tag in JSF, The Merge Tag, and BorderLayout in Java.