Struts 2 Tag

Struts 2 Tag

In this article, I am going to discuss Struts 2 Tag with Examples. Please read our previous article where we discussed Struts 2 Annotations.

What are Tags in Struts 2?

Struts 2 tags are used to manipulate the data displayed on a page. To use the Struts 2 tags on the view page, you must include a tag library directive. Typically, the taglib directive is <%@ taglib prefix=”s” uri=”/struts-tags” %>. So, the prefix for all the Struts 2 tags will be s.

One use of the Struts 2 Tags is to create links to other web resources, especially to other resources in the local application. While HTML provides a simple tag for creating hyperlinks, the HTML tag often requires us to include redundant information. Also, the HTML tag cannot easily access dynamic data provided by the framework.

There are four types of tags in struts 2:

  • Control tags
  • Data tags
  • Form tags
  • Ajax tags
What is a Control Tag?

Struts 2 control tags are used to control the flow of page execution, conditional processing, manipulating, iterating, and displaying the data. These tags allow developers to create dynamic responses in their web applications. The if and else tags perform basic conditional flow, while the iterator tag is used to generate a loop that iterates over each item in a collection. The bean tag instantiates a class that conforms to the JavaBeans specification, while the push tag pushes an object onto the stack. The i18n tag is used for the internationalization and localization of text messages.

Some examples of control tags are as follows:

  1. If and else
  2. Iterator
  3. Merge
  4. Append
  5. Generator
What is a Data Tag?

Struts 2 data tags are used to manipulate and display the data. These tags interact with the ValueStack to get and set the data. Data tags allow for data manipulation or creation, such as `bean`, `push`, `i18n`, and others. For example, the `bean` tag instantiates a class that conforms to the JavaBeans specification, while the `push` tag pushes an object onto the stack. The `i18n` tag is used for the internationalization and localization of text messages. These tags provide developers with a powerful toolset for creating dynamic responses in their web applications, allowing them to easily manipulate and display data in a variety of ways.

Some examples of data tags are as follows:

  1. Action
  2. Include
  3. Bean
  4. Date
  5. Param
  6. Property
  7. Push
  8. Set
  9. Text
  10. URL
What is a Form Tag?

The form tag in Struts2 is used to create an input form. It has numerous attributes that mimic the normal HTML form tag attributes. The action attribute sets the action name to submit to. The Struts 2 form tags include select, radio, checkbox, and checkbox list form controls. The example code for this tutorial, form-tags, can be checked out from struts-examples. The Struts 2 user mailing list is an excellent place to get help if you are having a problem getting the tutorial example applications to work. The form tag also allows for remote submission of the form without refreshing the page. The results from the form can be inserted into any HTML element on the page.

We have used such tags in previous projects, wherein we created forms in JSP files with the “s” prefix.

What is an Ajax Tag?

AJAX stands for Asynchronous JavaScript and XML. It allows JavaScript to make an HTTP request and update portions of a page directly, without going through a conventional POST or GET and refreshing the entire page. The key point is that when a script makes an “Ajax request” (XHR), the server doesn’t know it came from a script, and handles it like any other request. Instead of returning an entire page for the browser to display (or redisplay), an Ajax response will just return a portion of a page. The response can take the form of XML, or HTML, plain text, another script, or whatever else the calling script may want.

Struts2 uses the DOJO framework for the AJAX tag implementation. To use the AJAX tags, you must include the Dojo Plugin distributed with Struts 2 in your `/WEB-INF/lib` folder, add the taglib `<%@ taglib prefix=”sx” uri=”/struts-dojo-tags” %>` to your page, and include the `head` tag on the page.

The standard Struts 2 taglib includes several Ajax JSP tags, and many third-party libraries are available, including Ajax Tags, AjaxParts Taglib, ColdTags Suite, Prize Tags, and JSON-taglib. Alternatively, you can use a plain-old Ajax widget on a plain-old HTML page, using libraries like Dojo, JQuery, or YUI, and the StreamResult or the JSON Plugin.


The tags can in struts be described using the following diagram:

Struts 2 Tag

In the next article, I am going to discuss Struts 2 If Else Tag with Examples. Here, in this article, I try to explain the basic concepts of Struts 2 Tag and I hope you enjoy this Struts 2 Tag article.

Leave a Reply

Your email address will not be published. Required fields are marked *