Struts2 Login Page with Validation

Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller architecture. In this article, we will see how we can create a login page with validations using Struts 2. We will use struts UI tags to create a login page.

Before creating your Struts2 project it is important to make sure that you have the Apache Tomcat server installed and configured with the IDE of your choice like Eclipse. Now, if the above conditions are fulfilled then follow all the steps given below. We are going to use Eclipse IDE so that all the required components will be created under a Dynamic Web Project. Let us now start with creating Dynamic Web Project.

Creating Dynamic Web Project

First, you simply need to start your Eclipse IDE and create a new Dynamic Web Project project. To do this go to File > New > Dynamic Web Project and enter the project name as per your wish. I have named it “struts_validation”

<a href=creating dynamic web project" width="660" />

Set the rest of the options as given in the following screen

<a href=creating dynamic web project" width="775" />

Select all the default options on the next screens and don’t forget to check the Generate web.xml deployment descriptor option

web.xml deployment descriptor file

This will create a dynamic web project for you in Eclipse.

Setup Struts2 Libraries

Now to run the Struts2 application, you need to set up your Struts2 framework.

  1. You need to download and install Struts2 on your machine.
  2. Download the latest version of Struts2 binaries from here.
  3. At the time of writing this article, I downloaded struts-2.5.30-min-lib.zip.
  4. After unzipping the downloaded file, you will get files inside struts-2.5.30/lib as follows

struts-2.5.30 directory structure

Now copy all the files from the struts-2.2.3\lib folder to your project’s WEB-INF\lib folder. You can do this by simply dragging and dropping all the files into the WEB-INF\lib folder. Your project structure should now look like this

web.xml File:

Create a web.xml file under the webapp/WEB-INF folder and copy the following code in web.xml.