There are loads and loads of Struts 2 Tutorials on the web but most of them are confusing for beginners and mostly outdated. Here is a very simple, basic and easy to understand tutorial.
Before we start you must make sure that you too are working with Apache Tomcat version 7.0.xx or higher (Apache Tomcat version 9.0 is yet in Beta), make sure you have Eclipse IDE with J2EE plugin and you have downloaded the struts2 related jar files (http://mirror.cc.columbia.edu/pub/software/apache/tomcat/tomcat-7/v7.0.61/bin/apache-tomcat-7.0.61.tar.gz) and have created a Server under eclipse.
My Configurations and Setups :- Operating system: Mac OS X 10.10.2 (Yosemite)
- Application Server : Apache Tomcat version 7.0.61
- IDE: Eclipse Indigo Service Release 2 (20120216-1857)
This is a very simple example to get started, we will have an index.php page with two fields username and password, if username = "code2care" and password ="1234" the LoginAction lets you to Homepage.jsp or else to ErrorLogin.jsp, let's get started!
1. Create a new Project under Eclipse Project Explorer: SimpleStrutsEx
2. Place the index.jsp page under SimpleStrutsEx -> WebContent
index.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Simple Struts Example : Code2care</title>
</head>
<body>
<h1>Login Page : </h1>
<form action="loginForm">
<label for="uname">Enter UserName : </label>
<input type="text" name="uname"/><br/>
<label for="password">Enter Password : </label>
<input type="password" name="password"/><br/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
3. Create HomePage.jsp file under SimpleStrutsEx -> WebContent
homepage.jsp<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome : Admin</title>
</head>
<body>
Welcome : <s:property value="uname"/>
</body>
</html>
4. Create LoginError.jsp page and place it also under : SimpleStrutsEx -> WebContent
LoginError.jsp<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Login Error!</title>
</head>
<body>
No user found!! : <s:property value="uname"/>
</body>
</html>
5. Now lets create a package under SimpleStrutsEx -> Java Resources -> src and name it com.code2care.struts
6. Under the package, we just created in step 5 place our Action class called LoginAction.java
LoginAction.javapackage com.code2care.struts;
public class LoginAction {
private String uname;
private String password;
public String execute() throws Exception {
if(uname.equals("code2care") && password.equals("1234"))
return "SUCCESS";
else
return "ERROR";
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
7. Now lets create our web.xml file under : SimpleStrutsEx -> WebContent -> WEB-INF
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>StrutsExample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>loginProj</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginProj</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8. Now create a folder named classes under : SimpleStrutsEx -> WebContent -> WEB-INF
9. Create struts.xml under classes folder w created at step 8.
struts.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="SimpleStrutsEx" extends="struts-default" >
<action name="loginForm"
class="com.code2care.struts.LoginAction" method="execute">
<result name="SUCCESS">/HomePage.jsp</result>
<result name="ERROR">/LoginError.jsp</result>
</action>
</package>
</struts>
Note that you have added all Struts supporting jars under SimpleStrutsEx -> WebContent -> lib folder.
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.2.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.20.1.jar
xwork-core-2.3.20.1.jar
Source Code Download Link: Dropbox: SimpleStrutsEx.zip
Download PDF: Strut2 Simple Tutorial with Example.pdf
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- [Fix] zsh: command not found: awscli [Mac Terminal] - AWS
- Download Google Chrome setup exe file using PowerShell - Powershell
- New-SPLogFile PowerShell - create new SharePoint log file - SharePoint
- Two Ways to Extract rar (*.rar) files on Mac - MacOS
- Check version of pip package installer for Python - Python
- SharePoint - Use Today's Date Time in list view filter and calculated column - SharePoint
- Notepad++ Save Failed - Please check if this file is opened in another program. - NotepadPlusPlus
- How to fix Microsoft Windows 10 update error 80070020 - Microsoft