Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Text Tag
3.
Implementation
3.1.
Create Ninja Classes
3.2.
Create Views
3.3.
Configuration Files
3.4.
Output
4.
Frequently Asked Questions
4.1.
What exactly is Struts 2?
4.2.
What are the uses of the Struts 2 framework?
4.3.
Write some Common Data tags. 
4.4.
What is the Text Tag?
4.5.
How can you produce a WAR file?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Text Tag

Introduction

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.

Struts 2 is an MVC-based corporate Java web application development framework

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

 

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;
   }
}

 

Ninja Class

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>

 

Your web.xml file should look like this:

<?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 02</display-name>
   
   <welcome-file-list>
      <welcome-file>index1.jsp</welcome-file>
   </welcome-file-list>
   
   <filter>
      <filter-name>struts02</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

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

 

To produce a War file: 

  1. Right-click on the project name and select Export > WAR File
  2. After that, place this WAR in Tomcat's web apps directory. 
  3. Then launch Tomcat 
  4. Navigate to the URL http://localhost:8080/CodingNinjasStruts2/system.action.

Output

This will result in the screen shown below.
 

 

Output

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.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass