This is the second article in the series of Struts 2 Tutorials. If you have directly come here, I would recommend to check out earlier post too.

Struts 2 Beginners Tutorial

In last tutorial, we looked into the Struts 2 architecture, it’s components and build a simple Struts 2 web application with XML based configuration (struts.xml). In this tutorial we will see how we can avoid struts configuration file completely using annotations or naming conventions.

Struts 2 Convention Concept

Struts 2 uses two methodologies to find out the action classes and result classes. We need to use struts2-convention-plugin API to use any of these methodologies. If you have a normal web application, you can download it’s jar file and put it in the web application lib directory. For maven projects, you can simply add it’s dependency like below.

  1. Scanning: In this method, we specify package which needs to be scanned for action classes. The configuration needs to be done in web.xml for Struts 2 filter, like below.

    Struts 2 will find action classes by following methods.

    • Any class annotated with @Action or @Actions annotations.
    • Any class implementing Action interface or extending ActionSupport class.
    • Any class whose name ends with Action and contains execute() method. For these classes, naming convention is used to determine action and results.
  2. Naming Convention: Struts 2 will automatically create action for classes name ending with Action. The action name is determined by removing the Action suffix and converting first letter to lowercase. So if class name is HomeAction, then action will be “home”.If these classes are not annotated with @Result to provide the result, then result pages are looked into WEB-INF/content directory and name should be {action}-{return_string}.jsp. So if HomeAction action class is returning “success”, the request will be forwarded to WEB-INF/content/home-success.jsp page.Using naming convention alone can be very confusing and we can’t use same JSP page for other action classes. So we should try to avoid this and use annotation based configuration.

Now we are ready to create our Hello World struts 2 application using annotations and we won’t have struts 2 configuration file.

Create a dynamic web project in Eclipse Struts2AnnotationHelloWorld and convert it to maven project. The final project looks like below image.


Maven Configuration

We have added struts2-core and struts2-convention-plugin dependencies in the pom.xml, final pom.xml code is:

Deployment Descriptor Configuration

Notice the init-param element where we are providing action classes package that will be scanned by struts 2.

Result Pages

We have three result pages in our application.




Now let’s create our Action classes that we will annotate to configure action and result pages.

Action Classes with Annotations

Notice that HomeAction is an empty class with only purpose to forward the request to login.jsp page.

Notice the use of @Action, @Actions, @Result, @Namespace and @Namespaces annotations, the usage is self explanatory.

Now when we run our application, we get following response pages.

If you have read the last post where we have developed the same application with struts.xml configuration, you will notice that it’s almost same. The only change is the way we wire our application action classes and result pages.

By admin

Leave a Reply

%d bloggers like this: