Struts Intro

10

Click here to load reader

Transcript of Struts Intro

Page 1: Struts Intro

Implementation Struts

http://jakarta.apache.org/struts/index.html Framework for well-architectured web

applications Model-View-Controller design pattern

Model: application state View: presentation of that data Controller: handles routing of app Separates three elements distinctly

Page 2: Struts Intro

Model – View – Controller

Page 3: Struts Intro

Struts Framework provides

Central controller – ActionServlet All paths go through controller Configurable through xml

Action classes Adaptors between business layer and

web tier Can unit test at this level

ActionForms Hold data for each jsp page Auto-magically updated

Page 4: Struts Intro

Struts – continued Framework provides…

Tag libraries Avoid scriptlets Page designers can handle

Built In Internationalization Build In Validation of user input

Advantages Excellent architecture and design Reuse, modularity, extensibility etc.. Established framework Open source

Page 5: Struts Intro

Struts – general Architecture Each task a path Path contains info to do that task:

Action class whose perform() method will be called

Form Bean to hold data to / from view Forwards to pass control to on success /

failure Input jsp – auto validate user input

Graphically:

Page 6: Struts Intro

Page 1

Controller

Action2.java

Struts-config.

xml

Mappings

Business Logic Layer

Data Layer

View Layer

BusinessBean 1

Jsp Engine

Jsp 1

Action3.java

Action4.java

Action1.java

BusinessBean 2

BusinessBean 3

Jsp 2 Jsp 3

FormBean

1FormBean

2OtherBean

1

request/session

Web Browser

MarkVision Server

• path• action• [form bean]• [forwards]

Jsp 2

Web Server

Jsp 2

CoreSession CoreSession Socket

Page 7: Struts Intro

Example Step through show and save

logging parameters Note extra beans which

encapsulate interaction with server LoggingBean.java ServerBean.java

Page 8: Struts Intro

Web Server

creates / reuses

index.htm

Controller

ShowLoggingAction.java

Mappings

Business Logic Layer

Data Layer

View Layer

Jsp Engine

LoggingBean.java

logging.jsp

LoggingForm

request/session

Web Browser

Granite

Request:ShowLogging.do

creates form bean in correct scope

perform(mapping, form, request, response)

calls business logic ruleto get logging info

returns forward(success)

mainpulates data

setCategoryList(list)

forwards – logging.jsp

expands custom tags

response sent to browser – pure html

looks up

<!-- Show logging --><action path="/ShowLogging" type="beans.ShowLoggingAction" name="LoggingForm"> <forward name="success“ path="/logging.jsp" /></action>

ServerBean.java

populates form values

<form name="LoggingForm" …> <logic:iterate id="category“ name="LoggingForm" property="categoryList"> <bean:write name="category" property="key"/> <bean:write name="category" property="desc"/> …. </logic:iterate></form>

logging.jsp

MessagesResources

displays internationalized messages

Page 9: Struts Intro

Web Server

creates / reuses action instance

logging.jsp

Controller

ShowLoggingAction.java

Mappings

Business Logic Layer

Data Layer

View Layer

Jsp Engine

LoggingBean.java

results.jsp

LoggingForm

request/session

Web Browser

Granite

form submit – action=‘SaveLogging.do’

reuses form bean in correct scope

calls perform(mapping, form, request, response)

getCategoryList()

returns mapping.findForward("success")

forwards – result.jsp

expands custom tagsDisplays internationalized messages from bean

response sent to browser – pure html

looks up

<!-- Save logging --><action path="/SaveLogging" type="beans.SaveLoggingAction" name="LoggingForm"> <forward name="success“ path="/result.jsp" /></action>

ServerBean.java

MessagesBean

add result messages

auto populates using bean getters/setters

<form name="LoggingForm" …> <select name="category[0].value" ></form> becomes request parametercategory[0].value added to form usingLoggingForm.getCategory(0).setValue(value)

For each one that user has changed…

call business logic to change on server

••

•• •

••browser adds form variables to request

results.jsp

Page 10: Struts Intro

if submit, auto populates form bean from request params

Page 1

Controller

Action2.java

Struts-config.

xml

Mappings

Business Logic Layer

Data Layer

View Layer

BusinessBean 1

Jsp Engine

Jsp 1

Action3.java

Action4.java

Action1.java

BusinessBean 2

BusinessBean 3

Jsp 2 Jsp 3

FormBean

1FormBean

2OtherBean

1

request/session

Web Browser

Business Data

reads on start-up

creates

incoming requests

creates/reuses any associated form bean

• path• action• [form bean]• [forwards]

passes control to relevant action to handle

interacts with lower layers - acts as adaptor between HTTP and layers below

gets data to display (adds to beans in request/session)…or saves data from beans via business rules

looks up path to determine action/form bean

returns appropriate forward

relevant page called

processes custom tags – fill form elements from beans, display internationalized messages

pure HTML sent to browser

Jsp 2

Action2.java

FormBean

2••

• ••• •

Web Server

Jsp 2