Вы находитесь на странице: 1из 4

Step by Step Guide for building a simple for building a simple Struts Application

Sample App, We are going to build:

Steps
1. Create development directory structure
2. Write web.xml
3. Write struts-config.xml
4. Write ActionForm classes
5. Write Action classes
6. Create ApplicationResource.properties
7. Write JSP pages
8. Build, deploy, and test the application

Step 1: Create Development Directory Structure

Development Directory Structure


 Same development directory structure for any typical Web application
 Ant build script should be written accordingly
 If you are using NetBeans, the development directory structure is automatically created

Step 2: Write web.xml Deployment Descriptor


 Same structure as any other Web application
ActionServlet is like any other servlet
Servlet definition and mapping of ActionServlet needs to be specified in the web.xml

 There are several Struts specific


<init-param>elements
–Location of Struts configuration file
 Struts tag libraries could be defined

Example: web.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
3<servlet>
4<servlet-name>action</servlet-name>
5<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
6<init-param>
7<param-name>config</param-name>
8<param-value>/WEB-INF/struts-config.xml</param-value>
9</init-param>
10...
11</servlet>
12<servlet-mapping>
13<servlet-name>action</servlet-name>
14<url-pattern>*.do</url-pattern>
15</servlet-mapping>

Step 3: Write struts-config.xml

struts-config.xml

 Identify required input forms and then define them as<form-bean> elements


 Identify required Action's and then define them as<action> elements within <action-mappings> element
–make sure same value of name attribute of<form-bean> is used as the value ofnam e attribute of
<action> element
–define if you want input validation
 Decide view selection logic and specify them as <forward> element within <action> element

struts-config.xml: <form-beans>

1<?xml version="1.0" encoding="UTF-8" ?>


2
3<!DOCTYPE struts-config PUBLIC
4"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
5 "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
6
7
8 <struts-config>
9 <form-beans>
10 <form-bean name="submitForm"
11 type="submit.SubmitForm"/>
12 </form-beans>

struts-config.xml: <action-mappings>

1
2<!--= == = Action Mapping Definitions=== = == = == == = == =-->
3 <action-mappings>
4
5 <action path="/submit"
6 type="submit.SubmitAction"
7 name="submitForm"
8 input="/submit.jsp"
9 scope="request"
10 validate="true">
11 <forward name="success" path="/submit.jsp"/>
12 <forward name="failure" path="/submit.jsp"/>
13 </action>
14
15 </action-mappings>

Step 4: Write ActionForm classes

ActionForm Class

 Extend org.apache.s truts.ac tion.ActionForm class


 Decide set of properties that reflect the input form
 Write getter and setter methods for each property
 Write validate() method if input validation is desired

Write ActionForm class


package submit;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class SubmitForm extends ActionForm
{
/* Last Name */
private String lastName = "Hansen"; // default value
public String getLastName()
{
return (this.lastName);
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/* Address */
private String address = null;
public String getAddress()
{
return (this.address);
}
public void setAddress(String address)
{
this.address = address;
}
}

Вам также может понравиться