struts 2 introduction. struts 2 framework struts 2 a full-featured web application framework for the...

10
Struts 2 introduction

Upload: angelica-armstrong

Post on 05-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Struts 2 introduction

Page 2: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Struts 2 framework

Struts 2 A full-featured web application

framework for the Java EE platform

The Java Servlet API exposes the HTTP client / server protocol to the Java platform. Struts 2 is built on top of the Java Servlet API layer.

A framework makes generalizations about the common tasks and workflow of a specific domain. It then attempts to provide a platform upon which applications of that domain can be quickly built.

Struts 2

Java Servlet specification

HTTP

Page 3: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

History of Struts

Struts 2 Is from Apache software

foundation ASF is a non-profit corporation

(decentralized community of developers) supporting Apache software projects including the Apache HTTP server.

Struts 2 is a brand new web application framework

It is not just a new release of the Struts 1 framework, but a completely new framework based on the OpenSymphony WebWork framework.

• Struts 2 implements the popular Model-View-Controller (MVC) design pattern.

• The MVC pattern provides a separation of concerns that applies well to web applications.

• Separation of concerns allows us to manage the complexity of large software systems by dividing them into high-level components

Page 4: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Struts 2 implementation of the MVC Pattern

HTTP request

Web browserclient

ControllerFilterDispatcher

ModelAction

ViewResult

invoke action

render page

select result

Struts 2 MVC is realized by three core framework components : actions, results, and the FilterDispatcher

Page 5: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Struts 2 Request processing

Struts 2 request processing uses interceptors that fire before and after the action and result

Action Resultinvoke result

ActionContext (ThreadLocal)

ValueStack request session …

OGNL

invoke action finished

interceptors

OGNL

Page 6: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Declarative architecture [1]

Declarative architecture Is a type of configuration

that allows developers to describe their application architecture (configure) at a higher level than direct programmatic manipulation

Configuration occurs at two levels in Struts

As the Struts framework is flexible, it allows the developer to tweak its behaviour in many areas. This activity is actually configuring the behaviour of the Struts framework

The more important type of configuration (or declarative architecture) refers to defining the Struts 2 components that are used by your application and wiring / linking them together to form your desired workflow paths

Workflow paths refer to which action fires when a particular URL is hit, and which results might be chosen by that action to complete processing.

Page 7: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Declarative architecture [2]

Multiple methods There are two methods of

declaring the architecture of the application being developed

Through XML based configuration files

Through java annotations Whether the declaration is

in XML or using java annotations, the framework translates them both into the same run time components

With XML, one has the familiar XML configuration document with elements describing the application actions, results and interceptors.

struts.xml With java annotations, the

XML is gone. The metadata is located as java annotations inside the java source code for the classes that implement the Struts 2 actions. An annotated source file is shown below.

HelloWorldAction.java

Page 8: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

URL mapping to struts actions [helloworldxml project]

http:// + localhost:8080 + /helloworldxml + <package name space> + /HelloWorld.action

Protocol hostname:portnumber servlet context package name space action name.action

The URL combines the servlet context with the package namespace and the action name. Note that the action name takes the .action extension. If the package name space is specified, that should be used as part of the struts action identifier URL, else it should be spaces.

Page 9: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Struts 2 data flow trace for the helloworldxml applicationForm rendered by index.jsp

<s:form action="/strutshello/HelloWorld.action"> <s:textfield name="userName" label="User Name"/><s:submit /></s:form>

ValueStack

HelloWorld ActionString userNameString messageForm rendered by success.jsp

<h1><s:property value="message" /></h1>

Page 10: Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the

Struts 2 configuration

Configuration files web.xml

Is a J2EE configuration file that determines how the elements of the HTTP request are processed by the servlet container. It is not a Struts 2 configuration file, but it is a file that needs to be configured for Struts to work

struts.xml Contains the configuration

information that developer will be modifying as actions are developed. This is created under the directory WEB-INF/classes.

• struts-config.xml• The struts-config.xml

configuration file is a link between the View and Model components in the Web Client but you would not have to touch these settings for 99.99% of your projects.

• struts.properties• This configuration file provides

a mechanism to change the default behaviour of the framework. This file should be created in the WEB-INF/classes folder.Any line starting with # in this file is treated as comments.