Sameer’s Blog

How to write a simple struts application…

Posted by: seesameer on: April 5, 2008

The Struts project, an open−source project sponsored by the Apache
Software Foundation, is a server−side Java implementation of the
Model−View−Controller (MVC) design pattern.

The Struts projectwas originally created by Craig McClanahan in
May 2000.The Struts project was designed with the intention of
providing an open−source framework for creating Web applications
that easily separate the presentation layer and allow it to be
abstracted from the transaction/data layers. Since its inception,
Struts has received quite a bit of developer support, and is
quickly becoming a dominant factor in the open−source community.
All Struts applications are packaged using the Java Web
application format. Therefore,before we continue,let’s take a
brief look at Java Web applications.

Java Web applications are best described by the Java Servlet
Specification 2.4,which introduced the idea using the following
terms: “A Web Application is a collection of servlets, HTML pages,
classes, and other resources that can be bundled and run on
multiple containers from multiple vendors.”

In simpler terms, a Java Web application is a collection of one
or more Web components that have been packaged together for the
purpose of creating a complete application to be executed in the
Web layer of an enterprise application.Here is a list of the common
components that can be packaged in a Web application:Servlets,
Java Server Pages, custom tag libaries, java classes, configuration
files,static html files

Directory structure of web application ( Taking Tomcat as example…)

JSP pages are stroed in this directory
..<CATALINA_HOME>/webapps/APPLN_NAME/

classes are stored in <CATALINA_HOME>/webapps/APPLN_NAME/WEB-INF/classes/

configuration files tag libraries are stored in
<CATALINA_HOME>/webapps/APPLN_NAME/WEB-INF/

library files are stored in
<CATALINA_HOME>/webapps/APPLN_NAME/WEB-INF/lib/

The backbone of all Web applications is its deployment descriptor.
The Web application deployment descriptor is an XML file named
web.xml that is located in the
<CATALINA_HOME>/webapps/APPLN_NAME/WEB−INF/ directory.
The web.xml file describes all of the components in the Web
application.

Before starting development with struts we should get
struts-blank.zip from apache.Uncompress the Struts archive to your
local disk.Create a new Web application, using the directory structure
described above..

Struts is modeled after the MVC design pattern, you can follow a
standard development process for all of your Struts Web applications.
This process begins with the identification of the application Views,
the Controller objects that will service those Views, and the Model
components being operated on. This process can be described using
the following steps:

Define and create all of the Views, in relation to their purpose,
that will represent the user interface of our application.

Add all ActionForms used by the created Views to the struts−config.xml
file.

Create the components of the application’s Controller.

Define the relationships that exist between the Views and the
Controllers (struts−config.xml).

Make the appropriate modifications to the web.xml file; describe the
Struts components to the Web application.

Run the application.

Views… inpuname.jsp and greeting.jsp

inputname.jsp

<%@ taglib uri=”/WEB-INF/struts-html” prefix=”html” %>

<html:html>
<head>
<title>Struts Hello World</title>
</head>
<body>
<html:form action=”/greeting.do”>
<table border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td><b>Input name:</b></td>
</tr>
<tr>
<td>
<html:text property=”name” />
<html:submit value=” Say Hello! ” />
</td>
</tr>
</table>
</html:form>
</body>
</html:html>

greeting.jsp

<html>
<head>
<title>Greeting Struts Application</title>
</head>
<body>
<table border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td>
<h1><%=request.getAttribute(“greeting”)%></h1>
</td>
</tr>
</table>
</body>
</html>

struts-config.xml file

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE struts-config PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 1.2//EN”
“http://struts.apache.org/dtds/struts-config_1_2.dtd”>
<struts-config>
<data-sources/>
<form-beans>
<form-bean name=”Form” type=”sameer.Form”/>
</form-beans>
<global-exceptions/>
<global-forwards>
<forward name=”name” path=”/pages/inputname.jsp”/>
</global-forwards>
<action-mappings>
<action name=”Form” path=”/greeting” scope=”request” type=”sameer.GreetingAction”>
<forward name=”sayhello” path=”/pages/greeting.jsp”/>
</action>
</action-mappings>
<controller/>
</struts-config>

java classes..

package sameer
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

public class Form extends ActionForm {
private String name=”";

public Form() {
}

public void reset(ActionMapping actionMapping, HttpServletRequest request) {
this.name=”";
}

public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest request) {
ActionErrors errs = new ActionErrors();
return errs;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = (name==null?”":name);
}
}

Action class

package sameer

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class GreetingAction extends org.apache.struts.action.Action {

// Global Forwards
public static final String GLOBAL_FORWARD_getName = “name”;

// Local Forwards
private static final String FORWARD_sayhello = “sayhello”;

public GreetingAction() {
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = ((sameer.Form)form).getName();
String greeting = “Hello, “+name+”!”;
request.setAttribute(“greeting”, greeting);
return mapping.findForward(FORWARD_sayhello);
}
}

Leave a Reply

Blog Stats

  • 607 hits

 

April 2008
M T W T F S S
« Mar   Jun »
 123456
78910111213
14151617181920
21222324252627
282930