Walking on water and developing software from a specification are easy if both are frozen.

Struts Tutorial

In this tutorial you will learn how to create a Struts hello world application in eclipse. First create a new project, go to File->New and select DynamicWebProject.

Enter the project name and click the Finish button.

Add the following jar files to the WEB-INF\lib directory.

Right click the src folder and select New->Package.

Enter the package name as com.vaannila.form and click Finish.

Now right click the newly created package and select New->Class.

Enter the class name as HelloWorldForm and the superclass name as org.apache.struts.action.ActionForm and click Finish.

In the HelloWorldForm class add the following code.

01.package com.vaannila.form;
02. 
03.import org.apache.struts.action.ActionForm;
04. 
05.public class HelloWorldForm extends ActionForm {
06. 
07.private static final long serialVersionUID = -473562596852452021L;
08. 
09.private String message;
10. 
11.public String getMessage() {
12.return message;
13.}
14. 
15.public void setMessage(String message) {
16.this.message = message;
17.}
18.}

In the same way create a new package com.vaannila.action and create a HelloWorldAction class extending org.apache.struts.action.Action. Add the following code to the action class and save it.

01.package com.vaannila.action;
02. 
03.import javax.servlet.http.HttpServletRequest;
04.import javax.servlet.http.HttpServletResponse;
05. 
06.import org.apache.struts.action.Action;
07.import org.apache.struts.action.ActionForm;
08.import org.apache.struts.action.ActionForward;
09.import org.apache.struts.action.ActionMapping;
10. 
11.import com.vaannila.form.HelloWorldForm;
12. 
13.public class HelloWorldAction extends Action {
14. 
15.@Override
16.public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
17.HelloWorldForm hwForm = (HelloWorldForm) form;
18.hwForm.setMessage("Hello World");
19.return mapping.findForward("success");
20.}
21.}

Here we typecast the ActionForm to HelloWorldForm and set the message value.

Add the following entries in the struts-config.xml file.

01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02. 
03.<!DOCTYPE struts-config PUBLIC
04."-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
05."http://struts.apache.org/dtds/struts-config_1_3.dtd">
06. 
07.<struts-config>
08. 
09.<form-beans>
10.<form-bean name="helloWorldForm" type="com.vaannila.form.HelloWorldForm"/>
11.</form-beans>
12. 
13.<global-forwards>
14.<forward name="helloWorld" path="/helloWorld.do"/>
15.</global-forwards>
16. 
17.<action-mappings>
18.<action path="/helloWorld" type="com.vaannila.action.HelloWorldAction" name="helloWorldForm">
19.<forward name="success" path="/helloWorld.jsp" />
20.</action>
21.</action-mappings>
22. 
23.</struts-config>

Now configure the deployment descriptor. Add the following configuration information in the web.xml file.

01.<?xml version="1.0" encoding="UTF-8"?>
02.<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_2_5.xsd" id="WebApp_ID" version="2.5">
03.<display-name>StrutsExample1</display-name>
04. 
05.<servlet>
06.<servlet-name>action</servlet-name>
07.<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
08.<init-param>
09.<param-name>config</param-name>
10.<param-value>/WEB-INF/struts-config.xml</param-value>
11.</init-param>
12.<load-on-startup>2</load-on-startup>
13.</servlet>
14. 
15.<servlet-mapping>
16.<servlet-name>action</servlet-name>
17.<url-pattern>*.do</url-pattern>
18.</servlet-mapping>
19. 
20.<welcome-file-list>
21.<welcome-file>index.jsp</welcome-file>
22.</welcome-file-list>
23.</web-app>

When we run the application the index.jsp page will be executed first. In the index.jsp page we redirect the request to the helloWorld.do URI, which inturn invokes the HelloWorldAction.

1.<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
2.<logic:redirect forward="helloWorld"/>

In the action class we return the ActionForward "success" which is mapped to the helloWorld.jsp page. In the helloWorld.jsp page we display the "Hello World" message.

01.<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
02.<html>
03.<head>
04.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05.<title>Hello World</title>
06.</head>
07.<body>
08.<bean:write name="helloWorldForm" property="message"/>
09.</body>
10.</html>

After creating all the files the directory structure of the application looks like this.

On executing the application the "Hello World" message gets displayed to the user.

You can download the source code of this example by clicking on the Download link below.

Source :Download
War :Download

  MVC Struts Architecture
  Hello World Example in Eclipse
  Hello World Application
  Login Application Using Action Form
  DispatchAction Class
  LookupDispatchAction Class
  Using WildCards in ActionMapping Tutorial
  Multiple Struts Configuration Files Tutorial
  Struts Displaytag TableDecorator
  Export to Excel, PDF, CSV and XML using Display tag
  Pagination and Sorting using Display tag
  Export WorkBook to Excel
  Export WorkBook to Excel
  Export JSP Page to Excel
  Internationalizing Struts Application
  Internationalizing Struts Application i18
  Internationalizing Struts Application i18
  Tiles
  Struts Custom Validation Example
  Struts Mask Validation Rule Example
  Struts Date Validation Example
  Client-Side JavaScript Validation
  Highlighting Error Fileds
  Validator Framework
  DynaActionForm Bean
  Struts Logic Tag
  Struts HTML Tag
  Struts Selected HTML Tag
  Struts File Upload




affiliate_link