Back to: Java Struts Tutorials
Steps to create Struts 2 Application
In this article, I am going to discuss the steps to create a Struts 2 Application with an example. Please read our previous article where we discussed the Struts 2 Environment Setup. At the end of this article, you will understand the following pointers in detail.
- Steps to create Struts 2 Application Example
- Create the directory structure
- Create input page
- Provide the entry of Controller in (web.xml) file
- Create the action class
- Map the request with the action in (struts.xml) file and define the view
- Components
- Create view components
- load the jar files
- Start the server and deploy the project
Steps to create Struts 2 Application Example
Without using any IDE if you follow the below steps then we can easily create a Struts2 application –
- Create the directory structure
- Create input page (index.jsp)
- Provide the entry of Controller in (web.xml) file
- Create the action class (Login.java)
- Mapping of the request with the action in (struts.xml) file and defining the view components
- Create view components (welcome.jsp)
- load the jar files
- start the server and deploy the project
Create the directory structure
Below is the directory structure –
Create input page (index.jsp)
We can create a form with the help of the JSP page using struts UI tags. w\We need to specify uri/struts-tags to use UI tags. Here, we used s:form to create a form, s:textfield to create a textfield, and s:form to submit button.
index.JSP
<%@ taglib uri="/struts-tags" prefix="s" %> <s:form action="Login"> <s:textfield name="id" label="Email Id"></s:textfield> <s:textfield name="first_name" label="First Name"></s:textfield> <s:textfield name="password" label="Password"></s:textfield> <s:submit value="save"></s:submit> </s:form>
Provide the entry of Controller in (web.xml) file
StrutsPrepareAndExecuteFilter class acts as the controller. As we know, a filter is used by Struts2 for the controller, which is internally provided by the struts framework.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Create the action class (Login.java)
We need to create a simple bean class. Action is POJO (Plain Old Java Object) in struts2 and it has an extra method named as execute() i.e. by default invoked by struts framework.
Login.java
package com.info; public class Login { private int id; private String first_name; private String password; public int getId () { return id; } public void setId (int id) { this.id = id; } public String getFirst_Name () { return first_name; } public void setFirst_Name (String first_name) { this.first_name = first_ name; } public float getPassword () { return password; } public void setPassword (String password) { this.password = password; } public String execute () { return "success"; } }
Mapping of the request in (struts.xml) file and defining the view components
The struts.xml file provides information about the action to the struts framework, on the basis of the information struts framework takes a decision about the proper result invocation.
- struts element is the root element of this file, which represents an application.
- package element is the sub-element of struts, which represents a module of an application. Usually, it extends the struts-default package, which contains many interceptors and result types.
- The action element is the sub-element of a package, which represents an action that is to be invoked for the incoming request. It has a name, class, and method attributes. If you forget to specify the name attribute, then the execute() method is called for the specific action class by default.
- result element is the sub-element of action, which represents a view/result that will be invoked. Struts framework looks for the returned string by the action class, if it returns success, then the result page for the action is called whose name can be successful or without any name. It has option attributes like name and types. If you forgot to specify the result name, then success is considered as a result name by default. If you forgot to specify the type attribute, then dispatcher is assumed as the default result type by default.
struts.xml
?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default"> <action name="Login" class="com.Info.Login"> <result name="success">welcome.jsp</result> </action> </package> </struts>
Create view components (welcome.jsp)
To display the information, which we are getting using struts tags. So, it is basically a view component. The s:property returns the values of action objects like id, first_name, password.
welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s" %> Login Id:<s:property value="id"/><br/> First Name:<s:property value="first_name"/><br/> Password:<s:property value="password"/><br/>
Load the jar files
We will need the necessary jar files to run the application. Here, we are providing all the jar files. Download it and keep it in the lib folder of your project.
download the struts2 jar files
Start the server and deploy the project
Lastly, we need to start the server and deploy our application.
In the next article, I am going to discuss Creating First Struts 2 Example. Here, in this article, I try to explain the Steps to create Struts 2 Application and I hope you enjoy this Steps to create the Struts 2 Application article.