Home > Tutorials > Getting Started > How To Create A Struts 2 Web Application > Create Struts 2 Web Application With Artifacts In WEB-INF lib and Use Ant To Build The Application > Hello World Using Struts 2 > Using Struts 2 Tags |

Apache Struts 2 Documentation
Using Struts 2 Tags
Edit Page Browse Space Add Page Add News
Icon

This tutorial assumes you've completed the Hello World tutorial and have a working helloworld project. The example code for this tutorial, using_tags, is available for checkout from the Struts 2 GitHub repository at https://github.com/apache/struts-examples. The example projects use Maven to manage the artifact dependencies and to build the .war files.

In the Hello World lesson, we added to the index.jsp a Struts 2 url tag to create a hyperlink to the hello.action. This tutorial will explore the url and other Struts 2 tags further.

Web applications differ from conventional websites in that web applications can create a dynamic response. To make it easier to reference dynamic data from a page, the Struts 2 framework offers a set of tags. Some of the tags mimic standard HTML tag while providing added value. Other tags create non-standard, but useful controls.

Icon

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".
If you want to actually read the Struts 2 tag TLD file, you'll find it in the META-INF folder of the Struts 2 core jar.

### Struts 2 url Tag

One use of the Struts 2 Tags is to create links to other web resources, especially to other resources in the local application.

Icon

While HTML provides a simple `a` 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.

A very common use case in web applications is linking to other pages. In the Hello World tutorial we added to index.jsp a link to the hello.action using the Struts 2 url tag. Please refer to the url documentation for more information about the url tag.

index.jsp

When you run the Hello World tutorial in your Servlet container and then mouse over the Hello World hyperlink created by the Struts 2 url tag you'll see that the URL created is hello.action (relative to the web context's root folder).

Examine the struts.xml configuration in the Hello World tutorial and you will find this:

struts.xml

The action node above maps the hello.action to the execute method of class HelloWorldAction. If the execute method returns success, the view page HelloWorld.jsp (in web context root folder) will be returned to the user.

A common use case is that the URL also needs to include a value for a query string parameter such as userName. To add a query string parameter and its value use the Struts 2 param tag, nested inside the url tag.

For the Using Tags tutorial add the following to index.jsp just after the link for Hello World.

url tag with param

Rather then put the url tag as the value for the anchor tag's href attribute, we've separated out the s:url tag into its own code block. Nested inside the url tag is the Struts 2 param tag. This tag lets you specify a parameter name (e.g. userName) and a value for that paramter (e.g. Bruce Phillips).

Notice the use of the var attribute. The value of the var attribute is a reference we can use later in our code to refer to the url created.

Examine the anchor tag above. Notice the value of the href attribute is ${helloLink}. The view page will substitute the hyperlink we created using the url tag for the ${helloLink} reference. Note that the query string parameter will be properly URL-encoded (Bruce+Phillips).

Icon

In the next tutorial we'll cover how Struts can access the query string parameter value.

### Struts 2 Form Tags

Most applications will use several data entry forms. The Struts 2 tags make creating input forms easy. Consult the Form Tags Reference for all the details about the Struts 2 form tags.

Each of the Struts 2 form tags has numerous attributes to mimic the normal HTML form tag attributes.

To create the outer shell of the form, use the Struts 2 form tag. The action attribute sets the action name to submit to.

Add the following markup to index.jsp after the Hello Bruce Phillips link.

Struts 2 Form

The Struts 2 textfield tag provides a input html tag of tag text and the submit tag creates a submit button. When the index page is return by the server to the browser you should see:

The Struts form, textfield, and submit tags were converted to this HTML.

Struts Form Tags Converted To HTML

Note how Struts 2 created a table inside the form to position the form elements. In later tutorials you'll learn how to specify the layout (table, CSS). The Struts 2 textfield tag created an HTML input tag of type text with a name value that matches the name value of the textfield tag. Struts 2 also created a label HTML tag based on the label value of the textfield tag.

Icon

In the next tutorial we'll cover how to use Struts 2 to process this form submission.

### Struts 2 property tag

In the Hello World tutorial's example application on JSP HelloWorld.jsp was this code:

Struts Property Tag

The most common use of the property tag is used to "get" the value returned by calling a public get method (of the Action class) and then to include that value in the HTML returned to the browser.

As discussed in the Hello World tutorial, the value of "messageStore.message" instructs Struts 2 to first call method getMessageStore of the Action class. That method call returns a MessageStore object. The ".message" part instructs Struts 2 to call the getMessage method of the MessageStore object. The getMessage method returns a String which will be included in the HTML returned to the browser.

One very useful feature of the Struts 2 property tag is that it will automatically convert the most common data types (int, double, boolean) to their String equivalents. To demonstrate this feature let's add a static int variable to class HelloWorldAction.

Add Static Field

Each time the execute method is called we'll increase helloCount by 1. So add this code to the execute method of class HelloWorldAction.

Increase helloCount

Whenever a user clicks one of the links on page index.jsp (or submits the form), method execute of class HelloWorldAction will be run and the static field helloCount will be increased by one.

To include the value of the helloCount attribute in the HelloWorld.jsp we can use the Struts 2 property tag. Add the following to HelloWorld.jsp after the h2 tag.

Use Property Tag To Display helloCount Value

So even though the getHelloCount method returns an integer type, Struts 2 converted it to type String and placed it into the body of the p tag.

Icon

Note that even though helloCount is a static field, the get and set methods for helloCount are not static. For Struts 2 to call the getHelloCount method to get the value of helloCount, the getHelloCount method cannot be static.

If the value returned by the get method is an object, then the property tag will cause Struts 2 to call the object's toString method. Of course, you should always override Class Object's toString method in your model classes. Add the following toString method to the MessageStore class:

Add toString Method To Class MessageStore

Add the following to HelloWorld.jsp

Using Property Tag to Call toString

Since getMessageStore of HelloWorldAction class returns a MessageStore object, Struts 2 will call the toString method of class MessageStore. The string returned by that toString method will be displayed in the browser.

We covered a lot in this tutorial, but we've really only scratched the surface of how to use the Struts 2 tags. Consult the Struts 2 Tag Reference for much more information about all the Struts 2 tags.

Next | Onward to Coding Struts 2 Actions
Prev | Return to Hello World Using Struts 2

Children Show Children Hide Children
$page.link($child) (Apache Struts 2 Documentation)