Posted on by & filed under Content - Highlights and Reviews, Programming & Development, Web Development.

Apache Struts is one of the most popular web development frameworks, and Struts 2 adds significant functionality to the framework. Struts 2 supports action configuration via conventions and annotations, so components can now be written using annotations or XML. Large, distributed applications will always work best when components are written in XML, but small to medium web applications benefit from components written using annotations, since they are easier to write and maintain. This post provides a brief overview of annotations and XML for writing components in Struts 2 using small code snippets.

Component Development Using Annotations

Annotations are straightforward, since they allow you to annotate your Java Servlets (.java source files). This meta-data enables you to define controls right within your controller servlets. If you need more information about the basic configuration of Apache Struts 2, read this chapter in Apache Struts 2 Web Application Development.

Annotations are preceded by the “@” character followed by the instruction/annotation or interface’s name. For example:

  • Namespace Annotation (@Namespace)

    Define an action’s name space in Action class

  • Result Annotation (@Result)

    Define action’s results in an action class

  • Results Annotation (@Results)

    Define multiple results

  • After Annotation (@After)

    Marks an action method that needs to be called after the main action method

  • Before Annotation (@Before)

    Marks an action method that needs to be called before the main action method.

To read a HelloWorld using annotations, take a look at this section in Struts 2 in Action.

Let’s take an example. This code snippet has an annotation that takes a user name as input and prints out a “Hello, user_name” message.

Create class, using this code:

There are getter/setter methods along with an @Action annotation that defines the name “/hello” and the resulting JSP’s relative path “/jsp/welcome.jsp”. Create index.jsp, using the following code:

Finally, create success.jsp, which shows the result string:

In, define the relative directory path:

Component Development Using XML

Now let’s look how we do this using XML. First, you need to configure the Struts filter. Add the following code in web.xml:

Then configure filter mapping for the Struts 2 filter:

Now write the Action class. Create and put in this code:

This is similar to, except that the execute() now fetches the message through the XML element “HelloWorld.message”. Now in struts.xml, put in the following code:

And finally in the View (HelloWorld.jsp), put in this code snippet:

You can find out more on developing web components and programming using the Apache Struts 2 framework in the books listed below.

Safari Books Online has the content you need

Check out these Struts books available from Safari Books Online:

Struts 2 In Action introduces the Apache Struts 2 web application framework and shows you how to quickly develop professional, production-ready modern web applications. Written by Don Brown, one of the leading developers of Struts 2, Chad Davis, a passionate Struts 2 developer, along with Scott Stanlick, this book gently walks you through the key features of Struts 2 in example-driven, easy-to-digest sections. It delivers accurate, seasoned information that can immediately be put to work.
Struts 2.1 is a modern, extensible, agile web application framework suitable for both small- and large-scale web applications. Apache Struts Web Application Development begins with a comprehensive look at Struts 2.1 basics, interspersed with detours into more advanced development topics. You’ll learn about configuring Struts 2.1 actions, results, and interceptors via both XML and Java annotations. You’ll get an introduction to most of the Struts 2.1 custom tags and learn how they can assist in rapid application prototyping and development.
Offering both theoretical explanations and real-world applications, Struts 2 Design and Programming: A tutorial covers the 2.0 version of Struts, revealing how to design, build, and improve Java-based Web applications within the Struts development framework. Feature functionality is explained in detail to help programmers choose the most appropriate feature to accomplish their objectives, while other chapters are devoted to file uploading, paging, and object caching.

About the author

Umair Khan Jadoon is an avid developer. He enjoys building awesome apps for mobile and web and occasionally writing about technology. Geek at heart, he’s working on establishing his own mobile app startup.

Tags: annotations, Apache Struts, Apache Struts 2, XML,

Comments are closed.