Struts

26
Introduction to Jakarta Struts Framework

Transcript of Struts

Introduction to Jakarta Struts Framework

The Evolution of Server Side Web Ap Developement

CGI(or ISAPI) Servlet JSP JSP+Bean (Model 1) JSP+Bean+XML techs Model 2 (MVC) Model 2X(MVC with XML techs)

What is MVC The Model View Controller is a technique used to separate

Business logic/state (the Model) from User Interface (the View) and program progression/flow (the Control).

This pattern is very useful when it comes to modern web development:– The majority of modern, high usage websites are

dynamically driven.– People well skilled at presentation (HTML writers) seldom

know how to develop back-end solutions and visa versa.– Separating business rules from presentation is good no

matter what environment you develop in be it web or desktop.

資料來源 Craig W. Tataryn ,Introduction to MVC and the Jakarta Struts Framework

Benefits of MVC Promotes Modularity I18n Abstraction Allows application to be defined in a

flow-chart, use-case or activity diagram which is more easily transferred to implementation.

資料來源 Craig W. Tataryn ,Introduction to MVC and the Jakarta Struts Framework

The Struts Framework overview

Struts is an open source MVC framework developed by the Apache Jakarta project group.

Struts allows JSP/Servlet writers the ability to fashion their web applications using the MVC design pattern.

By designing your web application using Struts you allow:– Architect the ability to design using the MVC pattern– Developer to exploit the Struts framework when building

the app.– Web designer can learn how to program in MVC.

資料來源 http://jakarta.apache.org/struts/

The Struts Framework overview(2)

Struts takes much difficult work out of developing an MVC based web app.

The Struts framework provides a collection of canned objects which can facilitate fundamental aspects of MVC, while allowing you to extend further as need suites

Components of Struts Framework

Model– Business Logic Bean(or session

bean) 、 StateBean(or entity bean) 、 ActionForm 。 View

– A Set of Tag library.– Language Resource File

Controller– ActionServlet( controlled by struct-configs.xml)– ActionClasses

Utility Classes

Components of Struts Framework(2)

資料來源 http://www-106.ibm.com/developerworks/library/j-struts/?n-j-2151

How Struts works

資料來源 http://www-106.ibm.com/developerworks/library/j-struts/?n-j-2151

View The view in Struts is represented by

JSP pages. Minimize the occurance of scriptlets in

your page

– Use the tag-library provided by Struts to control the presenatational logic.

View:I18n The ability to maintain a web app in several

different languages Based on the language setup on the client

side, the web app will automatically switch. This is achieve in Struts through Java

Resource Bundles Instead of having to make a ResourceBundle

class for each language supported by your web app, resource bundles can be described as language resource files

Traditional Chinese English

Demo

View:I18n(2) Language Resource

– FileName Conventions

(ResourceName)_(ISO-LanguageCode)_(ISO-ConturyCode).properties – Register the Resource Name in web.xml.

In order for web developers to get at the string resources we use a special Struts tag called <bean:message/>

View:Example Login.jsp ApplicationResources_en_US.properti

es ApplicationResources_zh_TW.properti

es

Model ResultBean,hold information needed to

generate the result page. FromBean(extend ActionForm ),hold

the data passed by user either by POST or Query String.

Business Logic Bean. EJB 、 JMS or other components we

impl at business logic layer before.

Model:ActionForm JSP pages acting as the view for this LogonForm

are automatically updated by Struts with the current values of the UserName and Password properties.

If the user changes the properties via the JSP page, the LogonForm will automatically be updated by Struts

Before an ActionForm object is passed to a controller for processing, a “validate” method can be implemented on the form which allows the form to belay processing until the user fixes invalid input

Model:Example LoginForm.java tw.edu.nccu.user.TeacherData

Control The controller is the switch board of MVC. It directs the user to the appropriate views by providing the

view with the correct model The task of directing users to appropriate views is called

“mapping” in struts. The Struts framework provides a base object called

org.apache.struts.action.ActionServlet. The ActionServlet class uses a configuration file called

struts-config.xml to read mapping data called action mappings

The ActionServlet class reads the incoming URI and tries to match the URI against a set of action mappings to see if it can find a controller class which would be willing to handle the request

http://TeacherStudata/login.action

Server configured to pass *.action extensions to org.apache.struts.action.ActionServlet via a web.xml configuration file

ActionServlet object inspects the URI and tries to match it against an ActionMapping located in the struts-config.xml file.

Instance of appropriate Action class is found and it’s perform() method is called

Action object handles the request and returns control to a view based where the user is withinthe flow of the application

Controller (2)

Control:Example tw.edu.nccu.teacherstudata.actions.Lo

ginAction Struts-config.xml LoginAction.java

Problems with Struts Does not work on JDK1.4.0 Steep learning curve. Problems on old Application Servers(include

Tomcat3.2.1 、 Weblogic 5.1). The only output document is HTML , it

didn’t support the XSL transform technology.(Unless we modify the source code)

Model 2X Model2X

– Conceived by Julien Mercay and Gilbert Bouzeid on Feb 1,2002.

– Replace the JSP+TagLib with XML+XSLT at presentation layer.

– Using XSL Servlet as the Document Factory.

– Support pipe-line processing while generating the target document.

Resource Struts homepage

– http://jakarta.apache.org/struts/ Sun’s Servlet Specification

– http://java.sun.com/products/servlet/download.html#specs Sun’s JSP Specification

– http://java.sun.com/products/jsp/download.html Blue Stone’s Struts Tutorial

– http://developer.bluestone.com/scripts/SaISAPI.dll/Gallery.class/demos/trailMaps/index.jsp

My Struts page– http://java.cc.nccu.edu.tw/struts/