Struts 2 Hello World Example with Annotations and without struts.xml file

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.

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

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.

<filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
            <init-param>
                <param-name>actionPackages</param-name>
                <param-value>com.journaldev.struts2.actions</param-value>
            </init-param>
        </filter>


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.

Maven Configuration

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

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>Struts2AnnotationHelloWorld</groupId>
        <artifactId>Struts2AnnotationHelloWorld</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>

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

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <warSourceDirectory>WebContent</warSourceDirectory>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
            <finalName>${project.artifactId}</finalName>
        </build>
    </project>

Deployment Descriptor Configuration


<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
        xmlns="https://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>Struts2AnnotationHelloWorld</display-name>

        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
            <init-param>
                <param-name>actionPackages</param-name>
                <param-value>com.journaldev.struts2.actions</param-value>
            </init-param>
        </filter>

        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    </web-app>


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. login.jsp


<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<%-- Using Struts2 Tags in JSP --%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome User, please login below</h3>
<s:form action="login">
    <s:textfield name="name" label="User Name"></s:textfield>
    <s:textfield name="pwd" label="Password" type="password"></s:textfield>
    <s:submit value="Login"></s:submit>
</s:form>
</body>
</html>


error.jsp


<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Error Page</title>
</head>
<body>
<h4>User Name or Password is wrong</h4>
<s:include value="login.jsp"></s:include>
</body>
</html>


welcome.jsp


<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Welcome Page</title>
</head>
<body>
<h3>Welcome <s:property value="name"></s:property></h3>
</body>
</html>


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

Action Classes with Annotations

package com.journaldev.struts2.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

/**
 * An empty class for default Action implementation for:
 * 
 *  <action name="home">
 *		<result>/login.jsp</result>
 *	</action>
 * HomeAction class will be automatically mapped for home.action
 * Default page is login.jsp which will be served to client
 * @author pankaj
 *
 */

@Namespaces(value={@Namespace("/User"),@Namespace("/")})
@Result(location="/login.jsp")
@Actions(value={@Action(""),@Action("home")})
public class HomeAction extends ActionSupport {
}

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


package com.journaldev.struts2.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

/**
 * Notice the @Action annotation where action and result pages are declared
 * Also notice that we don't need to implement Action interface or extend ActionSupport
 * class, only we need is an execute() method with same signature
 * @author pankaj
 *
 */
@Action(value = "login", results = {
        @Result(name = "SUCCESS", location = "/welcome.jsp"),
        @Result(name = "ERROR", location = "/error.jsp") })
@Namespaces(value={@Namespace("/User"),@Namespace("/")})
public class LoginAction {

    public String execute() throws Exception {
        if("pankaj".equals(getName()) && "admin".equals(getPwd()))
            return "SUCCESS";
        else return "ERROR";
    }
    
    //Java Bean to hold the form parameters
    private String name;
    private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}


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.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

centron Managed Cloud Hosting in Deutschland

Standardize Configuration File Names for Log4j

Apache, Guide
Standardize Configuration File Names for Log4j Log4j expects configuration files to have standard names. If your files are named differently, such as myapp-log4j.xml or myapp-log4j.properties, log4j will not recognize them…