Step By Step Guide For Building A Simple Struts Application

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Step By Step Guide For Building A Simple Struts Application as PDF for free.

More details

  • Words: 1,004
  • Pages: 34
Step by Step Guide for building a simple Struts Application

1

Sang Shin [email protected] www.javapassion.com/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 struts-config.xml file Input validation Internationalizaition

You can also build it using NetBeans 4

Steps to follow

5

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 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 web.xml Deployment Descriptor

9

web.xml ●

Same structure as any other Web application – –



There are several Struts specific elements –



ActionServlet is like any other servlet Servlet definition and mapping of ActionServlet needs to be specified in the web.xml

Location of Struts configuration file

Struts tag libraries could be defined 10

Example: web.xml 1 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 5 <servlet-class>org.apache.struts.action.ActionServlet 6 7 <param-name>config 8 <param-value>/WEB-INF/struts-config.xml 9 10 ... 11 12 <servlet-mapping> 13 <servlet-name>action 14 *.do 15 11

Step 3: Write struts-config.xml

12

struts-config.xml ●



Identify required input forms and then define them as elements Identify required Action's and then define them as elements within element –

– ●

make sure same value of name attribute of is used as the value of name attribute of element define if you want input validation

Decide view selection logic and specify them as element within element 13

struts-config.xml: 1 2 3 4 5 6 7 8 9 10 11 12

<struts-config>

name="submitForm" type="submit.SubmitForm"/>

14

struts-config.xml: 1 2 3 4 5 11 12 13 14 15 15

Step 4: Write ActionForm classes

16

ActionForm Class ●







Extend org.apache.struts.action.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

17

Write ActionForm class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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; }

18

Write validate() method 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

public final class SubmitForm extends ActionForm { ... public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ... // Check for mandatory data ActionErrors errors = new ActionErrors(); if (lastName == null || lastName.equals("")) { errors.add("Last Name", new ActionError("error.lastName")); } if (address == null || address.equals("")) { errors.add("Address", new ActionError("error.address")); } if (sex == null || sex.equals("")) { errors.add("Sex", new ActionError("error.sex")); } if (age == null || age.equals("")) { errors.add("Age", new ActionError("error.age")); } return errors; }

19

Step 5: Write Action classes

20

Action Classes ● ●

Extend org.apache.struts.action.Action 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 javax.servlet.http.*; 4 import org.apache.struts.action.*; 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 = f.getLastName(); 16 // Translate the name to upper case 17 //and save it in the request object 18 request.setAttribute("lastName", lastName.toUpperCase()); 19 20 // Forward control to the specified success target 21 return (mapping.findForward("success")); 22 } 23 }

22

Step 6: Create ApplicationResource.properties and Configure web.xml accordingly

23

Resource file ● ●

Create resource file for default locale Create resource files for other locales

24

Example: ApplicationResource.properties 1 2 3 4 5 6 7

errors.header=

Validation Error(s)

    errors.footer=

error.lastName=
  • Enter your last name error.address=
  • Enter your address error.sex=
  • Enter your sex error.age=
  • 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: submit.jsp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

    <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> Submit example

    Example Submit Page

    Last Name:
    Address:
    Sex: Male Female
    Married:
    Age: 0-19 20-49 50-


    28

    Example: submit.jsp 1 2 3 4 5 6 7 8 9 10 11 12 13

    Hello young old

    29

    Step 8: Build, Deploy, and Test Application

    30

    Accessing Web Application

    31

    Accessing Web Application

    32

    Accessing Web Application

    33

    Passion!

    34

  • Related Documents