Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Struts 2 Example

In this example, we are creating the struts 2 example without IDE. We can simply create the struts 2 application by following these simple steps:

  1. Create the directory structure
  2. Create input page (index.jsp)
  3. Provide the entry of Controller in (web.xml) file
  4. Create the action class (
  5. Map the request with the action in (struts.xml) file and define the view components
  6. Create view components (welcome.jsp)
  7. load the jar files
  8. start server and deploy the project

1) Create the directory structure

The directory structure of struts 2 is same as servlet/JSP. Here, struts.xml file must be located in the classes folder.


2) Create input page (index.jsp)

This jsp page creates a form using struts UI tags. To use the struts UI tags, you need to specify uri /struts-tags. Here, we have used s:form to create a form, s:textfield to create a text field, s:submit to create a submit button.


  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. <s:form action=“product”>
  3. <s:textfield name=“id” label=“Product Id”></s:textfield>
  4. <s:textfield name=“name” label=“Product Name”></s:textfield>
  5. <s:textfield name=“price” label=“Product Price”></s:textfield>
  6. <s:submit value=“save”></s:submit>
  7. </s:form>

3) Provide the entry of Controller in (web.xml) file

In struts 2, StrutsPrepareAndExecuteFilter class works as the controller. As we know well, struts 2 uses filter for the controller. It is implicitly provided by the struts framework.


  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app>
  3.   <filter>
  4.   <filter-name>struts2</filter-name>
  5.    <filter-class>
  7.    </filter-class>
  8.   </filter>
  9.   <filter-mapping>
  10.    <filter-name>struts2</filter-name>
  11.     <url-pattern>/*</url-pattern>
  12.   </filter-mapping>
  13. </web-app>

4) Create the action class (

This is simple bean class. In struts 2, action is POJO (Plain Old Java Object). It has one extra method execute i.e. invoked by struts framework by default.

  1. package com.Kreationnext;
  2. public class Product {
  3. private int id;
  4. private String name;
  5. private float price;
  6. public int getId() {
  7.     return id;
  8. }
  9. public void setId(int id) {
  10. = id;
  11. }
  12. public String getName() {
  13.     return name;
  14. }
  15. public void setName(String name) {
  16. = name;
  17. }
  18. public float getPrice() {
  19.     return price;
  20. }
  21. public void setPrice(float price) {
  22.     this.price = price;
  23. }
  24. public String execute(){
  25.     return “success”;
  26. }
  27. }

5) Map the request in (struts.xml) file and define the view components

It is the important file from where struts framework gets information about the action and decides which result to be invoked. Here, we have used many elements such as struts, package, action and result.

struts element is the root elements of this file. It represents an application.

package element is the sub element of struts. It represents a module of the application. It generally extends the struts-default package where many interceptors and result types are defined.

action element is the sub element of package. It represents an action to be invoked for the incoming request. It has name, class and method attributes. If you don’t specify name attribute by default execute() method will be invoked for the specified action class.

result element is the sub element of action. It represents an view (result) that will be invoked. Struts framework checks the string returned by the action class, if it returns success, result page for the action is invoked whose name is success or has no name. It has name and type attributes. Both are optional. If you don’t specify the result name, by default success is assumed as the result name. If you don’t specify the type attribute, by default dispatcher is considered as the default result type. We will learn about result types later.


  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts
  3. Configuration 2.1//EN” “”>
  4. <struts>
  5. <package name=“default” extends=“struts-default”>
  6. <action name=“product” class=“com.Kreationnext.Product”>
  7. <result name=“success”>welcome.jsp</result>
  8. </action>
  9. </package>
  10. </struts>

6) Create view components (welcome.jsp)

It is the view component the displays information of the action. Here, we are using struts tags to get the information.

The s:property tag returns the value for the given name, stored in the action object.


  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Product Id:<s:property value=“id”/><br/>
  3. Product Name:<s:property value=“name”/><br/>
  4. Product Price:<s:property value=“price”/><br/>

7) Load the jar files

To run this application, you need to have the struts 2 jar files. Here, we are providing all the necessary jar files for struts 2. Download it and put these jar files in the lib folder of your project.

download the struts2 jar files

8) start server and deploy the project

Finally, start the server and deploy the project and access it.