HOME | J2ME | Struts | AJAX | SOAP | SOA MEDIA STREAMING AXIS |
Struts for Begginers
Introduction
MVC Design Pattern
Installation
Action Servlets
Hello World
Forms & ActionForms
Messages Resources
Data Bases
Exceptions
Acess ActionForms from JSP
Logic Tag

 

 

Struts for Beginners: Hello World Program

Back | Tutorial Home | Next

Lets follow the conventions and kick start the coding with a Hello World program.

HelloWorld.java

package com.salsa;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;

public class HelloWorld extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
		return mapping.findForward("hello");
	}
}

HelloWorld extends Action class which is the is a class which manipulated the Model. Here Models like request parameters, Java Beans can be manipulated.

Action class has a method called execute() which needs to be overridden in your program to make state changes to the Model.

ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) 

ActionMapping: This object holds all the mappings regarding this particular action like paths, forwards etc.
ActionForm: These are the request parameters associated with the request. We will take a closer look into this class later.
HttpServletRequest: Request Object
HttpServletRequest: Response Object.

Lets have a closer look at the action-mapping, We have just seen the HelloWorld class where the request is forwarded to “hello”. “Hello” here is alias to “helloWorld.jsp”

<action-mappings>
<action path="/helloWorld" type="com.salsa.HelloWorld">
<forward name="hello" path="/helloWorld.jsp"/>
</action>
</action-mappings>

Write a simple JSP file(helloWorld.jsp) which displays “Hello World”. Place this file in the salsa-tutorial folder in webapps.

<html>
<head>
<title>
Struts Classic Hello World
</title>
</head>
<body>
<h1>HelloWorld!!!!!</h1>
</body>
</html>

Deploy and run your first Struts program using URL http://localhost:8080/salsa-tutorial/helloWorld.do. if the browser displays “HelloWorld” then you have just successfully written your first Struts program.

The picture below shows the flow of your program

We will update this picture as the tutorial progresses.

Back | Tutorial Home | Next

site comments powered by Disqus
Download our free toolbar

toolbar powered by Conduit

| Copyright © 2009. All rights reserved | Terms and Conditions | About | Contact | Feed Back |