55
An Introduction to Building Web Application Using Building Web Application Using Jakarta Struts Framework Jakarta Struts Framework Presented to Indianapolis Java User Group 10/30/2002 Wei Li

Build Java Web Application Using Apache Struts

Embed Size (px)

DESCRIPTION

Build Java web application using Apache Struts MVC framework

Citation preview

Page 1: Build Java Web Application Using Apache Struts

An Introduction toBuilding Web Application Using Building Web Application Using

Jakarta Struts FrameworkJakarta Struts FrameworkPresented to Indianapolis Java User GroupPresented to Indianapolis Java User Group

10/30/2002

Wei Li

Page 2: Build Java Web Application Using Apache Struts

Challenge from Building Web Challenge from Building Web ApplicationsApplications

• Stateless protocol• Stateless protocol

• Multiple tiers

• …• …

Page 3: Build Java Web Application Using Apache Struts

Building Web Application with Building Web Application with Java - Why it is goodJava - Why it is good

• Build once, run everywhere (platform-• Build once, run everywhere (platform-independent, vendor-independent)

• Build it in an object-oriented way• Build it in an object-oriented way

• Reusable components

• Flexible and easy to be maintained and extended• Flexible and easy to be maintained and extended

Page 4: Build Java Web Application Using Apache Struts

Building Web Application with Building Web Application with Java - choicesJava - choices

• Pure Java Servlets (control + presentation)• Pure Java Servlets (control + presentation)

• Java Servlets (control) + template engine (presentation) (Velocity, WebMacro, Tea)(presentation) (Velocity, WebMacro, Tea)

• Java Servlets (control) + XML and XSLT (presentation)(presentation)

• JSP (presentation + control) + Java Beans (control+model) (Model 1)(control+model) (Model 1)

• JSP(presentation) + Java Servlets (control)+ Java Beans (model) (Model 2)Beans (model) (Model 2)

Page 5: Build Java Web Application Using Apache Struts

Problems with Pure JavaProblems with Pure JavaServlets- sample codeServlets- sample code

public void printHTMLReport(PrintWriter out, Results results) {

println(out, "<b>Poll:</b> " + poll.getId() + "<br>");

println(out, "<b>Question:</b> " + poll.getQuestion() + "<br>");

out.print("<b>Candidates:</b> ");out.print("<b>Candidates:</b> ");

this.printCandidates(out);

println(out, "<br>");

println(out, "<br>");println(out, "<br>");

println(out, "<b>The winner is...</b> " + results.getWinner() + "!<br>");

println(out, "<br><br>");

println(out, "Round-by-round summary:<br><br>");println(out, "Round-by-round summary:<br><br>");

}}

Page 6: Build Java Web Application Using Apache Struts

Problems with Pure JavaProblems with Pure JavaServletsServlets

• Servlets have to take care of presentation - which • Servlets have to take care of presentation - which is not a good thing for it to do

• HTML tags in the Java code• HTML tags in the Java code

• Hard to maintain and extend (every content change requires the compilation of Java classes)change requires the compilation of Java classes)

• Frustration of Java developers who are good at writing Java code but not HTMLwriting Java code but not HTML

Page 7: Build Java Web Application Using Apache Struts

Problems with JSPs + Java Beans Problems with JSPs + Java Beans (Model 1)(Model 1)

No controller - each page is responsible to direct control to the next page. next page.

request

JavaBeans

JSP

request

response EISBeans

Page 8: Build Java Web Application Using Apache Struts

Problems with JSPs + Java Beans Problems with JSPs + Java Beans (Model 1)(Model 1)

• Java code in HTML tags• Java code in HTML tags

• Hard to maintain and change also

• Frustration of content developer • Frustration of content developer

Page 9: Build Java Web Application Using Apache Struts

MVC and Model II

• Aim to separate the flow control, business logic and presentationand presentation

Page 10: Build Java Web Application Using Apache Struts

MVC and Model II

• Model - this is you data in the system. Such as a poll, the profile of a poll administrator.profile of a poll administrator.

• View - how data is represented to the user. For instance the view in a web application could be an HTML page, an image or other media.image or other media.

• Controller - is the glue between the model and the view. It is responsible for controlling the flow as well as It is responsible for controlling the flow as well as processing updates from the model to the view and visa versa

Page 11: Build Java Web Application Using Apache Struts

MVC and Model II

• Model II - uses a controller

1. requestWeb container

Controller1. request

2

Model

5. response

3

4

EIS

View5. response

Page 12: Build Java Web Application Using Apache Struts

MVC and Model II

• Controller - Java servelts• Controller - Java servelts

• Model - Java Beans (domain objects, value objects)objects)

• View - JSP, XML + XSLT ….

Page 13: Build Java Web Application Using Apache Struts

MVC and Model II

• Many implementations available for choice• Many implementations available for choice

• Expresso

• Barracuda• Barracuda

• Cocoon

• WebWork• WebWork

• Velocity, Tea, WebMacro• Velocity, Tea, WebMacro

• ….

Page 14: Build Java Web Application Using Apache Struts

MVC and Model II

• And of course, you can build your own• And of course, you can build your own

• But, Why? Regardless how you implement, you probably will end up with something similar or probably will end up with something similar or close to Struts.

Page 15: Build Java Web Application Using Apache Struts

Break 1

A good scientist is a person with original ideas. A A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible. works with as few original ideas as possible.

--- Freeman Dyson--- Freeman Dyson

Page 16: Build Java Web Application Using Apache Struts

Jakarta Struts - What is it?

• A flexible control layer based on standard technologies like Java Servlets, JavaBeans, ResourceBundles, and like Java Servlets, JavaBeans, ResourceBundles, and

Extensible Markup Language (XML).

• An implementation of MVC Model II

• A standardized and extensible web application • A standardized and extensible web application design/implementation framework

Page 17: Build Java Web Application Using Apache Struts

Jakarta Struts - General Info

• Craig McClanahan created the original framework and donated it to Apache Software Foundation (ASF) in 2000.donated it to Apache Software Foundation (ASF) in 2000.

• Current stable release - 1.0.2.

• 1.1 is in beta release.

• The framework provides 9 package with 250 classes• The framework provides 9 package with 250 classes

Page 18: Build Java Web Application Using Apache Struts

Jakarta Struts - Features

• An Model II implementation of MVC

• Big base packages and classes ready to use

• Rich Custom Tag Libraries ready to use• Rich Custom Tag Libraries ready to use

• Easy support for internationalization (I18N)

Page 19: Build Java Web Application Using Apache Struts

Jakarta Struts - the whole picture

Struts-config.xml

configuration

requestconfiguration

ActionServlet

action

Controller

Struts-config.xmlMessage Bundlerequest

actionactionaction

ModelEIS

View

JSP, Tag Library

….

Java Beans, EJB, ...

response

….

Page 20: Build Java Web Application Using Apache Struts

Jakarta Struts - the controllerJakarta Struts - the controller

• ActionServlet - Responsible for flow of control - 1. receive all requests, 2. route/delegate request to Action for receive all requests, 2. route/delegate request to Action for handling, 3. return response

• One instance per web application• One instance per web application

• Uses a configuration file called struts-config.xml to read mapping data, which specify control flow informationmapping data, which specify control flow information

• Provided by the framework already. Ready to use but can also be extendedalso be extended

Page 21: Build Java Web Application Using Apache Struts

Jakarta Struts - the controllerJakarta Struts - the controller

• Action - the actual class which processes the • Action - the actual class which processes the request relevant to the business operation. Based on action mappings defined in an XML on action mappings defined in an XML configuration file, the controller dispatches a call to the perform method of the appropriate Action to the perform method of the appropriate Action Class.

• Command pattern• Command pattern

• Framework provides some actions. You need to • Framework provides some actions. You need to implement you own action class for business purpose.purpose.

Page 22: Build Java Web Application Using Apache Struts

Jakarta Struts - the model

• You are responsible for creating it - Java Bean, • You are responsible for creating it - Java Bean, EJB, etc.

• Should be reusable domain objects and value • Should be reusable domain objects and value objects to represent a state.

• The framework works fine with any model • The framework works fine with any model implementation.

Page 23: Build Java Web Application Using Apache Struts

Jakarta Struts - the view

• JSP • JSP

• Custom Tag Library

• Resource Bundle• Resource Bundle

Page 24: Build Java Web Application Using Apache Struts

Jakarta Struts - the view

• Struts provides rich custom tag library to • Struts provides rich custom tag library to dramatically reduce Java code in your jsp.

• Example: HTML, BEAN, LOGIC, TEMPLATES,…TEMPLATES,…

• We will see how handy they are in the case study• We will see how handy they are in the case study

Page 25: Build Java Web Application Using Apache Struts

Jakarta Struts - the others

• ActionForm - encapsulate request/session data, • ActionForm - encapsulate request/session data, typically used for input forms, provides a central point to validate form data.point to validate form data.

Page 26: Build Java Web Application Using Apache Struts

Jakarta Struts - the others

• ActionMessage and ActionError• ActionMessage and ActionError

Page 27: Build Java Web Application Using Apache Struts

Jakarta Struts - the configuration

• Glue the system together• Glue the system together

• web.xml and struts-config.xml

Page 28: Build Java Web Application Using Apache Struts

Jakarta Struts - the configuration

• Web.xml: • Web.xml: o define the controller servlet (ActionServlet)with initial parameters;

o define URL mapping to be handled by the controller servlet;

define the Struts tag libraries.o define the Struts tag libraries.

Page 29: Build Java Web Application Using Apache Struts

Jakarta Struts - the configuration

• struts-config.xml • struts-config.xml o Glue all components together and set the rules for the

application

o Data Source Configuration

o Form Bean Definitionso

o Action Mapping Definitions

o Global Forward Definitionso Global Forward Definitions

Page 30: Build Java Web Application Using Apache Struts

Break 2

A manager, an engineer, and a developer are going down a hill in a jeep when it suddenly loses control and goes into a hill in a jeep when it suddenly loses control and goes into a ditch. They each attempt to offer a solution to the problem. The manager says 'Let's have a meeting to discuss a solution'. The engineer says 'Let's disassemble the jeep, and examine every part to find out where the problem is.' The developer says, 'Let's push it back up the hill and try The developer says, 'Let's push it back up the hill and try again.

http://www.middleware-company.com/offer/patternsday1/article4.shtml

Page 31: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• Introduction - When I was having a hard time to define an appropriate application to really have my hands wet with appropriate application to really have my hands wet with Struts, I spotted Alex Chaffee’s JiffyPoll (http://www.purpletech.com/irv/index.html ). I like his (http://www.purpletech.com/irv/index.html ). I like his idea but not all his implementation. Then I began to apply the struts framework to it and extends its functionality …the struts framework to it and extends its functionality …

Page 32: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• What does it do? • What does it do? o As a normal user - you can select and vote a poll, you

can view the results of any poll, you can also see the information about a poll.

o As an administrator - you can create a new poll right away. You can also disable/enable a poll owned by you.away. You can also disable/enable a poll owned by you.

Page 33: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• What does it do - Let us try it.• What does it do - Let us try it.

Page 34: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

Flat text file as persistence Web container

controller Service

as persistence media

PlainTextFilePollService

Service interface

requestcontroller Service

factoryDatabasePollService Relational

DatabaseEJBPollService

view model EJB Containerresponse view model EJB Container

Page 35: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• Benefits of the design • Benefits of the design o The real business logic is de-coupled from the

controller, the model and the view

o Increase of the reusability of the components - the service is completely reusable, the model (domain object and value object) is fully reusable also.object and value object) is fully reusable also.

Page 36: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• The controller - directly use ActionServlet • The controller - directly use ActionServlet provided by the framework

• The action classes - delegate to service provider which does the real work.which does the real work.

Page 37: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• The real business logic - de-coupled from the • The real business logic - de-coupled from the framework and stands as a independent/reusable componentcomponent

Page 38: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• The Model - reusable Java Beans, domain objects • The Model - reusable Java Beans, domain objects and value objects.

Page 39: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• The view - use the data provided in the model• The view - use the data provided in the model

• Rich use of tag library provide by the framework

• Show some sample JSPs and includes

• Almost no Java code in JSPs• Almost no Java code in JSPs

Page 40: Build Java Web Application Using Apache Struts

Case Study: Case Study: An Online Instant PollAn Online Instant Poll

• Internationalization - easy to be implemented like • Internationalization - easy to be implemented like a breeze

• Demo

Page 41: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• Promote clean separation of components• Promote clean separation of components

Page 42: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• Increase the modularity, reusability of components• Increase the modularity, reusability of components

Page 43: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• Focus on design• Focus on design

Page 44: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• Very use case driven• Very use case driven

Page 45: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• What you need to do is clear• What you need to do is clear

• You can really focus on the real work - the business logicbusiness logic

Page 46: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• Easy internationalization• Easy internationalization

Page 47: Build Java Web Application Using Apache Struts

Jakarta Struts - Why it is good

• Free• Free

• Open Source

• Implemented by savvy developer/expert• Implemented by savvy developer/expert

• large user community

Page 48: Build Java Web Application Using Apache Struts

New features in 1.1 - what makes New features in 1.1 - what makes it betterit better

• Declarative exception handling

<action path="/login"

type="net.indyjug.weili.struts.security.LoginAction"

name="loginForm"

scope="request"

validate="true"

input="/jsp/login.jsp">input="/jsp/login.jsp">

<exception key="error.invalidlogin"path=”/ jsp/login.jsp"path=”/ jsp/login.jsp"scope="request"type=”net.indyjug.weili.struts.exception.InvalidLoginException"/>

<forward name="success" path="/jsp/administration.jsp" /><forward name="success" path="/jsp/administration.jsp" />

<forward name="failure" path="/jsp/login.jsp" />

</action>

Page 49: Build Java Web Application Using Apache Struts

New features in 1.1 - what makes New features in 1.1 - what makes it betterit better

• Dynamic Form Bean• Dynamic Form Bean

Page 50: Build Java Web Application Using Apache Struts

New features in 1.1 - what makes New features in 1.1 - what makes it betterit better

• Plug-in• Plug-in

Page 51: Build Java Web Application Using Apache Struts

New features in 1.1 - what makes New features in 1.1 - what makes it betterit better

• Tiles• Tiles

Page 52: Build Java Web Application Using Apache Struts

The learning curve

• Not so easy for the first time. After that life is • Not so easy for the first time. After that life is easy.

• Manageable• Manageable

• Custom tag library may make mad at the beginning. Do not quit. They are so handy once beginning. Do not quit. They are so handy once you know how to use them.

Page 53: Build Java Web Application Using Apache Struts

Fast Track: Fast Track: Getting StartedGetting Started

• What you need• What you needo JDK 1.2 and above

o Web Contained compliant with Servlet API 2.2 and JSP o Web Contained compliant with Servlet API 2.2 and JSP API 1.1

o An XML parser compliant with JAXP 1.1 or above o(this is usually provided by the web container)

Page 54: Build Java Web Application Using Apache Struts

Fast Track: Fast Track: Getting StartedGetting Started

• The framework provides an application template • The framework provides an application template for you to use and extend

• Always continue on top of something which is • Always continue on top of something which is working.

• Build the whole structure first and leave the • Build the whole structure first and leave the concrete implementation later on (example)

Page 55: Build Java Web Application Using Apache Struts

Question?