![]() |
VOOZH | about |
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.
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"
Set the rest of the options as given in the following screen
Select all the default options on the next screens and don't forget to check the Generate web.xml deployment descriptor option
This will create a dynamic web project for you in Eclipse.
Now to run the Struts2 application, you need to set up your Struts2 framework.
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
Create a web.xml file under the webapp/WEB-INF folder and copy the following code in web.xml.
We need a JSP file to display the final result, this page will be called by the Struts 2 framework where a predefined action will be called and this mapping is defined in the struts.xml file. So we will now create an index.jsp under the webapp folder in your eclipse project. To create a JSP file, right-click on the webapp folder in the project explorer and select New >JSP File.
Now copy the following code into the index.jsp file
The <%@taglib uri="/struts-tags" prefix="s" %> is a directive that tells the Servlet Container that this page will be using the Struts2 tags and these tags will be preceded by s. The s:form tag contains all the form elements. When the user clicks on the Login button, the request is forwarded to the Login action class.
Create welcome.jsp under the webapp folder and copy the following code into welcome.jsp
Our Login class extends ActionSupport. It is good to extend ActionSupport class as it provides a default implementation for most common tasks. Create a Login.java class and Copy the following code into Login.java
We need a mapping between URL to action mapping. The struts.xml maps the Login class, and the welcome.jsp together. The mapping tells the Struts 2 framework which class will respond to the user's action (the URL), which method of that class will be executed, and what view to render based on the String result that method returns. So we will now create a struts.xml file under the webapp/WEB-INF folder. Copy the following code into struts.xml:
Here our “default” package extends the “struts-default” package. By extending the “struts-default” package the action will by default inherit the set of interceptors defined in the default stack. The “struts-default” package is defined in the struts-default.xml file.
Right-click on project > run as > run on server
If you're running your project for the first time you'll get a prompt as follows:
Now expand localhost and select your tomcat server. After this click on the next and finish. You will see a web page opened on your Eclipse IDE like this:
All the input fields are required. If you try to submit without entering details you will get a screen as follows:
The Phone Number should be 10 digits. If you enter an incorrect phone number, you'll get the following screen:
The default User Name is "Sanjyot" and password you can enter anything. After you enter the correct User Name you'll get the following screen: