0% found this document useful (0 votes)
23 views34 pages

Step by Step Guide For Building A Simple Struts Application

The document provides steps to build a simple Struts application. It describes creating forms, actions, configuration files, and JSP pages. The application allows submitting a form and displays validation errors or a success message.

Uploaded by

thangaprabhu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views34 pages

Step by Step Guide For Building A Simple Struts Application

The document provides steps to build a simple Struts application. It describes creating forms, actions, configuration files, and JSP pages. The application allows submitting a form and displays validation errors or a success message.

Uploaded by

thangaprabhu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Step by Step Guide

for building a simple


Struts Application

1
Sang Shin
[Link]@[Link]
[Link]/j2ee
Java™ Technology Evangelist
Sun Microsystems, Inc.
2
Sample App We are
going to build

3
Sample App
● Keld Hansen's submit application
● Things to do

– Creating ActionForm object


– Creating Action object
– Forwarding at either success or failure through
configuration set in [Link] file
– Input validation
– Internationalizaition
● You can also build it using NetBeans

4
Steps to follow

5
Steps
[Link] development directory structure
[Link] [Link]
[Link] [Link]
[Link] ActionForm classes
[Link] Action classes
[Link] [Link]
[Link] JSP pages
[Link], deploy, and test the application

6
Step 1: Create Development
Directory Structure

7
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

8
Step 2: Write [Link]
Deployment Descriptor

9
[Link]
● 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 [Link]
● There are several Struts specific
<init-param> elements
– Location of Struts configuration file
● Struts tag libraries could be defined

10
Example: [Link]
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.4" xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
3 <servlet>
4 <servlet-name>action</servlet-name>
5 <servlet-class>[Link]</servlet-class>
6 <init-param>
7 <param-name>config</param-name>
8 <param-value>/WEB-INF/[Link]</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>
11
Step 3: Write
[Link]

12
[Link]
● 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 of name attribute of
<action> element
– define if you want input validation
● Decide view selection logic and specify them as
<forward> element within <action> element
13
[Link]: <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 "[Link]
6
7
8 <struts-config>
9 <form-beans>
10 <form-bean name="submitForm"
11 type="[Link]"/>
12 </form-beans>

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

15
Step 4: Write
ActionForm classes

16
ActionForm Class
● Extend [Link]
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

17
Write ActionForm class
1 package submit;
2
3 import [Link];
4 import [Link].*;
5
6 public final class SubmitForm extends ActionForm {
7
8 /* Last Name */
9 private String lastName = "Hansen"; // default value
10 public String getLastName() {
11 return ([Link]);
12 }
13 public void setLastName(String lastName) {
14 [Link] = lastName;
15 }
16
17 /* Address */
18 private String address = null;
19 public String getAddress() {
20 return ([Link]);
21 }
22 public void setAddress(String address) {
23 [Link] = address;
24 } 18
Write validate() method
1 public final class SubmitForm extends ActionForm {
2
3 ...
4 public ActionErrors validate(ActionMapping mapping,
5 HttpServletRequest request) {
6
7 ...
8
9 // Check for mandatory data
10 ActionErrors errors = new ActionErrors();
11 if (lastName == null || [Link]("")) {
12 [Link]("Last Name", new ActionError("[Link]"));
13 }
14 if (address == null || [Link]("")) {
15 [Link]("Address", new ActionError("[Link]"));
16 }
17 if (sex == null || [Link]("")) {
18 [Link]("Sex", new ActionError("[Link]"));
19 }
20 if (age == null || [Link]("")) {
21 [Link]("Age", new ActionError("[Link]"));
22 }
23 return errors;
24 } 19
Step 5: Write
Action classes

20
Action Classes
● Extend [Link] class
● Handle the request
– Decide what kind of server-side Model objects
(EJB, JDO, etc.) can be invoked
● Based on the outcome, select the next view

21
Example: Action Class
1 package submit;
2
3 import [Link].*;
4 import [Link].*;
5
6 public final class SubmitAction extends Action {
7
8 public ActionForward execute(ActionMapping mapping,
9 ActionForm form,
10 HttpServletRequest request,
11 HttpServletResponse response) {
12
13 SubmitForm f = (SubmitForm) form; // get the form bean
14 // and take the last name value
15 String lastName = [Link]();
16 // Translate the name to upper case
17 //and save it in the request object
18 [Link]("lastName", [Link]());
19
20 // Forward control to the specified success target
21 return ([Link]("success"));
22 }
23 } 22
Step 6: Create
[Link]
and Configure [Link]
accordingly

23
Resource file
● Create resource file for default locale
● Create resource files for other locales

24
Example:
[Link]
1 [Link]=<h4>Validation Error(s)</h4><ul>
2 [Link]=</ul><hr>
3
4 [Link]=<li>Enter your last name
5 [Link]=<li>Enter your address
6 [Link]=<li>Enter your sex
7 [Link]=<li>Enter your age

25
Step 7: Write JSP pages

26
JSP Pages
● Write one JSP page for each view
● Use Struts tags for
– Handing HTML input forms
– Writing out messages

27
Example: [Link]
1 <%@ page language="java" %>
2 <%@ taglib uri="/WEB-INF/[Link]" prefix="bean" %>
3 <%@ taglib uri="/WEB-INF/[Link]" prefix="html" %>
4 <%@ taglib uri="/WEB-INF/[Link]" prefix="logic" %>
5
6 <html>
7 <head><title>Submit example</title></head>
8 <body>
9
10 <h3>Example Submit Page</h3>
11
12 <html:errors/>
13
14 <html:form action="[Link]">
15 Last Name: <html:text property="lastName"/><br>
16 Address: <html:textarea property="address"/><br>
17 Sex: <html:radio property="sex" value="M"/>Male
18 <html:radio property="sex" value="F"/>Female<br>
19 Married: <html:checkbox property="married"/><br>
20 Age: <html:select property="age">
21 <html:option value="a">0-19</html:option>
22 <html:option value="b">20-49</html:option>
23 <html:option value="c">50-</html:option>
24 </html:select><br>
25 <html:submit/>
28
26 </html:form>
Example: [Link]
1 <logic:present name="lastName" scope="request">
2 Hello
3 <logic:equal name="submitForm" property="age" value="a">
4 young
5 </logic:equal>
6 <logic:equal name="submitForm" property="age" value="c">
7 old
8 </logic:equal>
9 <bean:write name="lastName" scope="request"/>
10 </logic:present>
11
12 </body>
13 </html>

29
Step 8: Build, Deploy,
and Test Application

30
Accessing Web Application

31
Accessing Web Application

32
Accessing Web Application

33
Passion!

34

You might also like