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:
- If and else
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:
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?
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:
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.