Struts 2 Annotation Simple Login Example

By Arvind Rai, April 30, 2014
In this page, we will learn how to start with struts 2 by a simple login example using annotation. I have tried to explain step by step how to configure the struts 2 to use in our example. Struts 2 application uses action classes to process the request. Every action has a URL pattern. Action class redirects to JSP for displaying and taking input.

Software Used

For demo, we will run a simple login application. To run application we are using below softwares.
1. Eclipse
2. JDK 6
3. Tomcat 7

web.xml Configuration Struts 2

We will start writing our code from web.xml. Define filter tag for org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter. We need to pass actionPackages param . By this configuration Struts 2 scans the action classes which we create in our application.
web.xml
<web-app>
  <display-name>Struts 2 Annotation Example</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.concretepage.action</param-value>
        </init-param>
    </filter>
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app> 

Annotation based Action Classes Struts 2

In our Login Example there will be login , success and failure screen, first for taking login input from user and rest for displaying result of login process. In struts 2, a JSP will open via an action class. So to get Login Page, we will create an Action class.
LoginAction.java
package com.concretepage.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")
@Action("/login")
@ResultPath(value="/")
@Result(name="success",location="login.jsp")
public class LoginAction extends ActionSupport{
	public String execute() {
	    return SUCCESS;
	}
} 
Action class has an execute() method. That is executed first when the action class is called. com.opensymphony.xwork2.Action class provides keys like success and error to be returned as constant by execute() method. The annotations used in Login action are described below.
@Namespace: Struts 2 gets the path of action class.
@Action: Defines the names of action class to be used in URL.
@ResultPath: Defines the path to get JSP pages.
@Result: Gives the JSP page to be displayed.
Now when user enters login information and submits, another action class is called.
ResultAction.java
package com.concretepage.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="/")
@Action(value="result", results={@Result(name="success",location="result.jsp"),
		@Result(name="error",location="error.jsp")})
public class ResultAction extends ActionSupport{
	private String userName;
	private String pwd;
	public String execute() {
		if(getUserName() !=null && getUserName().equals(getPwd())){
		   return SUCCESS;
		}else{
			return ERROR;
		}
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
} 
We need to provide setter getter method for those JSP field whose value we need to fetch in action class. In our example, we are fetching user name and password, so we have written setter and getter method for username field and password field of login page.

Writing JSP for Login Example

To write JSP, we will use struts-tags tag library.
login.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Annotation Example</h1>
<s:form action="result">
	<s:textfield name="userName" label="User Name"/>
	<s:password name="pwd" label="Password"/>
	<s:submit/>
</s:form>
</body>
</html> 
To declare form, text and password field we need to use struts-tags library. In form tag, provide action name that will be called after submitting this page. Now find the success JSP page.
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Annotation Example</h1>
<h4>Your User Name is:  <s:property value="userName"/></h4>
</body>
</html> 
Use struts-tags library to get the property from action class. Find failure JSP page to display un authorization message.
error.jsp
<html>
<head></head>
<body>
<h1>Struts 2 Annotation Example</h1>
<h4> You are not authorized for the application. </h4>
</body>
</html>  

Application Structure in Eclipse

Find the application structure in eclipse.
Struts 2 Annotation Simple Login Example

Struts 2 JAR dependencies in pom.xml

To run the application, struts 2 needs the below JAR dependencies.
pom.xml
<dependencies>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-core</artifactId>
		<version>2.3.16</version>
	</dependency>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-convention-plugin</artifactId>
		<version>2.3.8</version>
	</dependency>
</dependencies>	 

Output screen of Login Example

Use URL http://localhost:8080/Struts2Demo-1/user/login to run the application. Find the Login Page. Enter any username and enter password same word used as user name, then we will get success page and if differ will get un authorization page.
Struts 2 Annotation Simple Login Example
Find the success page after login.
Struts 2 Annotation Simple Login Example
Find un authorization page.
Struts 2 Annotation Simple Login Example
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us