Struts 2 Annotation Login Example

In this Struts 2 tutorial, we will create a login application using Annotation. To create an annotation based application you need to include the struts2-convention-plugin-2.x.x.jar in your project.

Software Used

  1. NetBeans IDE
  2. Tomcat 7
  3. JDK 7

Required Dependencies

You can directly add all core JARs of struts2.x.x in your application or add below to your pom.xml if your application maven based.


Project Structure in NetBeans

Struts 2 Annotation Login Example

Note: If you are using Annotation, you need not to create the struts.xml file.

Annotation Snippet

@Namespace: Define the path of action class.

@ResultPath: Define the path of the JSP page.

@Action:: Define the name of the action that is used in the URL.

@Result: Define the JSP page to be displayed.

Define filter in web.xml

Define the struts 2 filters in web.xml and pass the action class by defining actionPackages.


Create Annotation based action class

In this class define the name of form variable and generate the getter and setters of variables. And define your business logic.
package org.websparrow.action;

import com.opensymphony.xwork2.ActionSupport;
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;

@Action(value = "register", results = {
    @Result(name = "success", location = "welcome.jsp"),
    @Result(name = "error", location = "index.jsp")})
public class AnnotationWelcomeAction extends ActionSupport {

    private String userId, pass, msg;

    public String execute() throws Exception {
        if (pass.equalsIgnoreCase("admin")) {
            return SUCCESS;
        } else {
            msg = "You have entered the wrong password. Try again...";
            return ERROR;

    public String getUserId() {
        return userId;

    public void setUserId(String userId) {
        this.userId = userId;

    public String getPass() {
        return pass;

    public void setPass(String pass) {
        this.pass = pass;

    public String getMsg() {
        return msg;

    public void setMsg(String msg) {
        this.msg = msg;


Create JSP pages

Create your form page and welcome page.

<%@taglib prefix="s" uri="/struts-tags" %>
        <h1>Struts 2 Annotation Login Example</h1> 
        <s:form action="register">
            <s:textfield name="userId" label="User Id"></s:textfield>
            <s:password name="pass" label="Password"></s:password>
            <s:submit value="Login"></s:submit>
        <p style="color: red;"><s:property value="msg"></s:property></p>
<%@taglib prefix="s" uri="/struts-tags" %>
        <h1>Struts 2 Annotation Login Example</h1>
        <p style="color: green;">Welcome, <s:property value="userId"></s:property>. You have successfully logged in.</p>


Now run your application and login with credentials. Check the below images for reference.

Screen 1

Struts 2 Annotation Login Example

Screen 2

Struts 2 Annotation Login Example

Screen 3

Struts 2 Annotation Login Example

Download Source Code –

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.