Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

The example code for this tutorial, Struts2_Annotations_Ant or Struts2_Annotations_Mvnannotations, is available on Google Code - httpfor checkout at https://code.googlegithub.com/papache/struts2-examples/downloads/listImage Removed. After downloading and unzipping the file, you'll have a folder named Struts2_Annoattions_Ant (or Struts2_Annotations_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example application.struts-examples

Introduction

In our previous tutorials we've been using an XML file (struts.xml) to configure our applicationapplications. The XML file wires up the action names (register), with ActionSupport classes (RegisterRegisterAction.java), and with the result to render back to the browser (register.jsp). Struts 2 provides an alternative to using XML to configure your application by using standard naming conventions and annotations for your action names, ActionSupport classes, and results.

Info

This tutorial assumes you understand how to use apply annotations to Java classes and methods. If you're not familiar with annotations, consult the Java online tutorial.

...

Struts 2 enables the use of a standard naming convention conventions and annotations by including when you include the Convention plugin in your application's class path. If you're using Maven you'll need to add a dependency:

Code Block
XMLxmlXML
xml
titleConvention Plugin Dependency


<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-convention-plugin</artifactId>
  <version>2.2.1</version>
</dependency>

...

Tip

The convention plugin provide several different ways you can configure your Struts 2 application without using XML. Consult the Convention Plugin documentation for complete details. This tutorial only examines one simple way of following the conventions provided by the Convention plugin.

If When you download run the example application and run it you'll see on the index.jsp you'll see page a link to Get your hello. This URL for the link is hello.action. When you click on this link, the execute method of class HelloAction.java (which is a Struts 2 ActionSupport class) is run. The view page rendered back to the browser after the execute method returns success is hello-success.jsp.

None of the above is wired up using XML but rather happens because the application follows the standard naming conventions expected by the Convention plugin. The first convention is that the ActionSupport class, HelloAction.java, is in package org.apache.struts.struts2annotations.action. One of the Convention plugin's defaults is to look for ActionSupport classes that are in package structure that ends in action. The next convention the application follows is that HelloHelloAction.java extends the ActionSupport class and defines an execute method. The link is hello.action. When the Struts 2 filter sees a request for hello.action it will map that request to the HelloAction class's execute method due to the Convention plugin being used.

So a link of hello.action causes the execute method of class HelloAction to be run. That method returns "success." Because the application is using the Convention plugin, Struts 2 will render back to the browser a view page named hello-success.jsp that is located in WEB-INF/content (by default the Convention plugin expects all view pages to be in this location). If the execute method returns "input" or "error" then the view page rendered would have been hello-input.jsp or hello-error.jsp.

...

In a previous tutorial we reviewed how to use the Struts 2 Configuration plugin to view the details of how Struts 2 has configured your application. When using the Convention plugin, it's very handy to also use the Configuration plugin during development. On the example application's home page is a link to the application's configuration. Click on that link and then the hello link on the left menu (under Actions in default). You'll see the configuration for the hello action including it's Action class, result, and view page.

Image Modified

Annotations

If you want to go beyond the simple naming convention conventions provided by the Convention plugin, you can use the Struts 2 annotations also provided by the plugin. For example, a common work-flow for a Struts 2 application is to first execute the ActionSupport class's input method to setup form field default values and then to run the execute method of the same ActionSupport class when the form is submitted (to validate and save the user's input).

The link to Register for the drawing on the example application's home page follows this work flow. The link value is register-input.action. If you examine the RegisterAction.java class you'll find the input method with an Action annotation.

Code Block
JAVAjavaJAVA
java
titleAction Annotation


@Action("register-input")
public String input() throws Exception {

	logger.info("In input method of class Register");
		
	return INPUT;
}

The Action annotation tells Struts 2 to execute the annotated method when the action link value equals the Action annotation's value ("register-input"). So a link of register-input.action will call the input method of class RegisterAction. On the example application's home page is a liink link to Register for the drawing with a URL of register-input.action.

...

In previous examples, we included in struts.xml values for some of the Struts 2 configuration parameters.

Code Block
XMLxmlXML
xml
titlestruts.xml parameter configuration


<constant name="struts.devMode" value="true" />

We When we don't use a struts.xml file, we can set the value of these Struts 2 parameters by using filter parameters in web.xml:

Code Block
XMLxmlXML
xml
titleStruts 2 Parameter Configurate web.xml


<filter>
  <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      <init-param>
	  <param-name>struts.devMode</param-name>
	  <param-value>true</param-value>
      </init-param>
</filter>

...

We've just scratched the surface of what the Struts 2 convention plugin provides to reduce or eliminate the need to use an XML file to configure your Struts 2 application. The Struts 2 Convention plugin provides ways to map multiple actions to the same method, map results to different view pages, map errors to view pages, and much more. Be sure to read through the Convention Plugin documentation for alternative ways to configure your Struts 2 application.