Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label Struts Framework. Show all posts
Showing posts with label Struts Framework. Show all posts

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 : http://struts.apache.org/2.3.4.1/index.html


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 'org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter'. 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="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	<filter>
		<filter-name>Struts2Filter</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>Struts2Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>



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;
	}

	@Override
	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"?>
<!DOCTYPE struts PUBLIC 
'-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN' 
'http://struts.apache.org/dtds/struts-2.0.dtd'>

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




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"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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>
</head>
<body>
<%@taglib prefix="s" uri="/struts-tags"%>	
	<h2>Enter Customer Details !!!</h2>
	<s:form method="get" action="HelloWorld">
		<table border="0" cellpadding="0" cellspacing="2">
			<tr>
				<td align="center">Enter your name:</td>
				<td align="left"><s:textfield theme="simple" name="customerName" /></td>
			</tr>
			<tr>
				<td align="center">Enter your email:</td>
				<td align="left"><s:textfield theme="simple" name="customerEmail" /></td>
			</tr>
			<tr>
				<td align="center">Enter your phone:</td>
				<td align="left"><s:textfield theme="simple" name="customerPhone" /></td>
			</tr><tr><td><br></td></tr>
			<tr>
				<td align="left" colspan="2"><s:submit value="Submit"
						theme="simple" /></td>
			</tr>
		</table>
	</s:form>
</body>
</html>


c. done.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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"%>
</head>
<body>
	<h2>Customer Details !!!</h2>
	<b>Customer Name : <s:property value="customerName" /></b>
	<br>
	<b>Customer Email : <s:property value="customerEmail" /></b>
	<br>
	<b>Customer Phone : <s:property value="customerPhone" /></b>
	<br>
</body>
</html>


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 ... 



 
© 2021 Learn Java by Examples Template by Hubberspot