struts overview

22
Struts Overview Shakeel Mahate [email protected]

Upload: elliando-dias

Post on 06-May-2015

4.604 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Struts Overview

Struts Overview

Shakeel [email protected]

Page 2: Struts Overview

What is Struts Framework to develop web

applications Open source Jakarta project http://jakarta.apache.org/struts/ Current release at 1.1 Beta 3 Based on MVC-Model 2 pattern

Page 3: Struts Overview

What is MVC-Model 2

Browser

Controller

Servlet

Model

JavaBean

View

JSPHTML Stylesheets

Form submitHTTP Event

HTTP ResponseGet property

Forward

Create/Set property

BusinessLayer

Page 4: Struts Overview

What is Struts MVC-Model 2

Browser

Controller

ActionServlet

Model

JavaBeanView

JSP

BusinessLayerActionForm

Struts-Config.

xml

BusinessLogic

Action

ResourceBundle

create

Http Event

Http Response

forward

dispatch

create/set

get

forward

Page 5: Struts Overview

What is ActionServlet Traffic cop Part of the Struts Framework Consults struts-config.xml to

determine Action – ActionForm mapping

Transforms request parameters into ActionForm Type safety, validation

Page 6: Struts Overview

What is Action Extend org.apache.struts.action.Action

Override execute() Talk to your business layer to

perform the action Populates data in request/session

scope Tells Struts which page to display

return mapping.findForward(“displayResults”);

Page 7: Struts Overview

What is struts-config.xml Similar to web.xml which

configures logical names for Servlets and specifies the Java class name

Configures action mappings, form beans, forwards (views), resource bundles, plugins

Page 8: Struts Overview

Resource Bundles Configures message resource bundles

<message-resources parameter=“myApplication”/>

Utilized to NLS enable application All static strings should be specified

in myApplication.properties file Format: key=value

Name.label=Enter first name

Page 9: Struts Overview

Global forwards Remember RequestDispatcher.forward()

method – hence the name forward!!! Logical name for all global filenames Level of indirection NO FILENAMES in execute()

<forward name=“displayResults”path=“/pages/default.jsp”/>

Page 10: Struts Overview

Action Mappings Maps a form name to an Action Every form has an ActionForm Associates Java class name to an

Action Associates an input page to an Action

Page that triggered this action Used for handling validation errors

Associates result pages (forwards) to an Action

Page 11: Struts Overview

Action Mapping<action path=“/createUser”

type=“… Java class name …”scope=“request | session | application”input=“another action or jsp”name=“form bean name”>

<forward name=“…” path=“…”/><forward name=“…” path=“…”/>

</action>

Utilized bymapping.

findForward()

/createUser.do

Page 12: Struts Overview

Form Beans Instantiated by ActionServlet

before invoking Action.execute() How does ActionServlet know

which form bean? How does it know which Java class?

Struts-config.xml<form-bean name=“…”

type=“… Java class name …”/>

Page 13: Struts Overview

Form Bean Instantiation ActionServlet checks the appropriate

scope for the form bean before calling class.newInstance()

Populates data from the request parameters and does type conversion

Check out BeanUtils in Jakarta Commons project

Page 14: Struts Overview

How to write Form Beans Zero argument constructor Public getters/setters DO NOT USE ENTITY BEANS as form

beans Use Data Transfer Objects (Value

Objects) Decouple web app from business

layer

Page 15: Struts Overview

View JSP or HTML pages Use Struts tag library, JSTL tag

library DO NOT HARD CODE labels

Utilize ResourceBundle Uses beans stored in request or

session or application scope

Page 16: Struts Overview

Simple View<html:form action=“… form bean name …”>

<bean:message key=“name.label”/><html:text property=“name”/><html:submit>

…Name of the button…</html:submit>

</html:form>

Page 17: Struts Overview

Validation Override validate() in ActionForm

public ActionErrors validate(ActionMapping map, HttpServletRequest req) {ActionErrors errors = new ActionErrors();if (name==null || name.length() < 1)

errors.add(“nameMissing”, new ActionError(“msg.key”));return errors;

}

Add the following to Input page<html:errors property=“nameMissing”/>

Page 18: Struts Overview

Validator package Automatically generate client side

JavaScript code Specify WEB-INF/validation.xml

Define regular expressions for validating data, phone#, social security#, etc.

For each form specify required fields Augment rules in

WEB-INF/validator-rules.xml

Page 19: Struts Overview

No need to write ActionForm Dynamically generate ActionForm

class for your forms Specify form fields in struts-

config.xml Basic idea is properties of a bean

are a HashMap Again check Jakarta Commons

Project

Page 20: Struts Overview

Modular Struts Typical web app development

consists of 4-5 programmers Struts-config.xml is the bottleneck Create Modules (or folders) with

their own struts-config.xml

Page 21: Struts Overview

Tiles Templates for Web pages Layout various tiles of a web page

Header, Navigation, Body, Footer Specified in tiles-definition.xml

Specifies the layout Logical names for various tiles Associate filenames to the tiles

Page 22: Struts Overview

How to develop Struts apps Use WSAD 5.0