In this tutorials, it will reuse the previous Strust 2 Hello World (XML version) example, and convert it into annotation version.

Struts 2 Annotation Concept

The Struts 2 annotation is supported by Struts 2 convention plugin, So, you have to understand the magic behind its “Scanning Methodology” and “Naming Converter” mechanism.

1. Scanning Methodology

Many Struts 2 articles or books stated that you can configure the filter’s “init-param” or “struts.convention.action.packages” to tell Struts 2 where to scan the annotated classes. For example,

web.xml

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  <init-param>
	<param-name>actionPackages</param-name>
	<param-value>com.mkyong.common</param-value>
  </init-param>
</filter>

From my testing (Struts2 version 2.1.6 and 2.1.8), this is not true, no matter what you put in the “param-value” or “struts.convention.action.packages“, Struts 2 will just ignore it and scan the specified folders named struts, struts2, action or actions only.

Here’s how the scanning work

  1. Scan the annotated classes which located at the packaged named “struts, struts2, action or actions“.
  2. Next, scan the file which match either of the following criteria :
    • Implemented the com.opensymphony.xwork2.Action interface.
    • Extends the com.opensymphony.xwork2.ActionSupport class.
    • File name ends with Action (e.g UserAction, LoginAction).

See this Struts 2 convention plugin documentation.

2. Naming Converter

Struts 2 convention plugin will convert all the annotated action file name to a specified format.

For example : LoginAction.java

  1. First, remove the “Action” word at the end of the file name, if present.
  2. Second, convert the first letter of the file name to lowercase.

So, After removing the ending and converting the case of the first letter, the LoginAction.action will change to login.action.

The Struts 2 convention plugin’s “scanning methodology” and “naming converter” features are really bring a lot of conveniences and benefits, only if your Struts 2 project is following the naming convention properly; otherwise it will be a total disaster.

Struts 2 Annotation example

It’s time to start the conversion process.

Final project structure

Struts2 hello world annotation

1. Update pom.xml

To use the Struts 2 annotation feature, you need to download the struts2-convention-plugin.jar.
pom.xml

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

2. LoginAction

Create a LoginAction extends ActionSupport and do nothing, the ActionSupport default return a “success” string, which will match the @Result and redirect to “pages/login.jsp“.

Annotation version

package com.mkyong.user.action;
 
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
 
import com.opensymphony.xwork2.ActionSupport;
 
@Namespace("/User")
@ResultPath(value="/")
@Result(name="success",location="pages/login.jsp")
public class LoginAction extends ActionSupport{
 
}

XML equivalent

<package name="user" namespace="/User" extends="struts-default">
	<action name="Login">
		<result>pages/login.jsp</result>
	</action>
</package>

3. WelcomeUserAction

Override the execute() method and specified the @Action and @Result annotation.

Annotation version

package com.mkyong.user.action;
 
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
 
import com.opensymphony.xwork2.ActionSupport;
 
@Namespace("/User")
@ResultPath(value="/")
public class WelcomeUserAction extends ActionSupport{
 
	private String username;
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	@Action(value="Welcome", results={
		@Result(name="success",location="pages/welcome_user.jsp")
	})
	public String execute() {
 
		return SUCCESS;
 
	}
}

XML equivalent

<package name="user" namespace="/User" extends="struts-default">
   <action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction">
	<result name="SUCCESS">pages/welcome_user.jsp</result>
   </action>
</package>
The Struts 2 annotations – @Action, @Result and @Namespace are self-explanatory, you can always compare it with the XML equivalent. The @ResultPath may need a bit explanation, see this @ResultPath example.

4. JSP view pages

Normal JSP view pages to accept a username and password, and redirect to a welcome page once the submit button is clicked.

login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Hello World Annotation Example</h1>
 
<s:form action="Welcome">
	<s:textfield name="username" label="Username"/>
	<s:password name="password" label="Password"/>
	<s:submit/>
</s:form>
 
</body>
</html>

welcome_user.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Hello World Annotation Example</h1>
 
<h4>Hello <s:property value="username"/></h4>
 
</body>
</html>

5. struts.xml

No need to create struts.xml file, all classes are annotated.

6. web.xml

Just create a classic web.xml file and declared the FilterDispatcher filter as normal.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Struts 2 Web Application</display-name>
 
  <filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
 
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

7. Run it

The LoginAction.action is changed to login.action, see “Naming Converter” above.
http://localhost:8080/Struts2Example/User/login.action

Struts 2 annotation login screen

http://localhost:8080/Struts2Example/User/Welcome.action

Struts 2 annotation welcome screen

Reference

  1. Struts 2 convention plugin documentation
  2. Strust 2 Hello World (XML version)
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts