Pin It

How to develop your first Struts 2 Application : Step by Step

A simple Web-Application demonstrating the use of Struts 2 Framework in Java EE. Let's create your first Struts 2 Framework Application using Eclipse IDE.

1. Create a new Dynamic Web Project in Eclipse -
See the Screenshot below for help :

Click next to reach screenshot below. After changing appropriate contents click on finish button to create a new Dynamic Web Project. 

2. Add the Struts Framework specific libraries to the lib folder inside web -> WEB-INF. -

The Struts jars can be downloaded from the website :

3. Add Struts 2 Filter in the Deployment Descriptor called as web.xml -

See the web.xml snippet below. It contains entry with the filter which has information about Struts 2 Filter. This tag has two sub-tags by name : 'filter-name' and 'filter-class'. 'filter-name' tag provides the name of the filter and 'filter-class' provides the class of the filter used. Here we provide Struts 2 Front Controller filer class as ''. There is yet another tag 'filter-mapping' which has mapping of the filter that after which url pattern the filter should come into picture to process the incoming request. Here the url-pattern tag provides the url to filter and it is mapped by its name in the filter-name tag with the filter-name tag of the filter tag. It should be placed under WEB-INF folder.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.0">


4. Create a Struts Action Class in Java

This Java class is simple in nature having few properties and its getter and setters method. It is just like a normal POJO class. It is having a method called as execute() method which is been called by the Struts Framework. The return type of this method is a String which tells Struts to which jsp or servlet the request needs to be forwarded. The result of execute method is mapped to a new jsp or servlet by providing information in the struts.xml configuration file. The Java code is given below :

package com.hubberspot.struts;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

	private String customerName;
	private String customerEmail;
	private Long customerPhone;

	public HelloWorld() {


	public String getCustomerName() {
		return customerName;

	public void setCustomerName(String customerName) {
		this.customerName = customerName;

	public String getCustomerEmail() {
		return customerEmail;

	public void setCustomerEmail(String customerEmail) {
		this.customerEmail = customerEmail;

	public Long getCustomerPhone() {
		return customerPhone;

	public void setCustomerPhone(Long customerPhone) {
		this.customerPhone = customerPhone;

	public String execute() {
		System.out.println("Reached here ... ");
		return "success";

6. Create struts.xml file for mapping result of Action class to Result page -

<?xml version="1.0" encoding="UTF-8"?>
'-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN' 

	<package name="default" namespace="/" extends="struts-default">
		<action name="HelloWorld" class="com.hubberspot.struts.HelloWorld">
			<result name="success">/done.jsp</result>

7. Create one css file and two jsps by name style.css, index.jsp and done.jsp -

a. style.css

	body {
		font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
		background-color: #FDF4DF;
		font-size: 14px;
		color: #333333;
	td {
		font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
		font-size: 14px;

b. index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="style.css" rel="stylesheet" type="text/css">
<title>Hello World Application</title>
<%@taglib prefix="s" uri="/struts-tags"%>	
	<h2>Enter Customer Details !!!</h2>
	<s:form method="get" action="HelloWorld">
		<table border="0" cellpadding="0" cellspacing="2">
				<td align="center">Enter your name:</td>
				<td align="left"><s:textfield theme="simple" name="customerName" /></td>
				<td align="center">Enter your email:</td>
				<td align="left"><s:textfield theme="simple" name="customerEmail" /></td>
				<td align="center">Enter your phone:</td>
				<td align="left"><s:textfield theme="simple" name="customerPhone" /></td>
				<td align="left" colspan="2"><s:submit value="Submit"
						theme="simple" /></td>

c. done.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer Confirm !!!</title>
<link href="style.css" rel="stylesheet" type="text/css">
<%@taglib prefix="s" uri="/struts-tags"%>
	<h2>Customer Details !!!</h2>
	<b>Customer Name : <s:property value="customerName" /></b>
	<b>Customer Email : <s:property value="customerEmail" /></b>
	<b>Customer Phone : <s:property value="customerPhone" /></b>

8. Place all the files created above as snapshot below -

9. Run the Application by running index.jsp and provide values and see output on submitting -

After the form has been submitted ... 

Please Give Us Your 1 Minute In Sharing This Post!
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
© 2012 Learn Java by Examples Template by Hubber Spot