Java Web Programming [7/9] : Struts2 Basics

19
Module 7: Struts 2 Basics Thanisa Kruawaisayawan Thanachart Numnonda www.imcinstitute.com

description

Presentation for Java Web Programming Course; 2011

Transcript of Java Web Programming [7/9] : Struts2 Basics

Page 1: Java Web Programming [7/9] : Struts2 Basics

Module 7: Struts 2 Basics

Thanisa Kruawaisayawan

Thanachart Numnonda

www.imcinstitute.com

Page 2: Java Web Programming [7/9] : Struts2 Basics

2

Apache Struts

Struts is an open source Web application framework developed as Apache Jakarta project http://jakarta.apache.org/struts/

Model-View-Controller (MVC) framework Used for constructing web applications based Servlets

and JSP technologiesStruts application is a genuine Web application that should

be able to run on any Servlet container including all Java EE compliant App servers

Page 3: Java Web Programming [7/9] : Struts2 Basics

3

Why Struts?

Takes much of the complexity out of building your own MVC framework

Encourages good design practice and modeling Easy to learn and use Feature-rich and many supported 3rd-party tools Flexible and extensible Large user community Stable and mature Open source

Page 4: Java Web Programming [7/9] : Struts2 Basics

4

How Struts 2 Works

Page 5: Java Web Programming [7/9] : Struts2 Basics

5

Our Goal

Page 6: Java Web Programming [7/9] : Struts2 Basics

6

Steps

1. Write JSP pages (View)

2. Write Action class (Model)

3. Forward at either success or error through configuration set in struts.xml

4. Build, deploy, and test the application

Page 7: Java Web Programming [7/9] : Struts2 Basics

7

Step 1: Write JSP pages Step 1: Write JSP pages

Page 8: Java Web Programming [7/9] : Struts2 Basics

8

JSP Pages

Write one JSP page for each view Use Struts tags for

Handing HTML input formsWriting out messages

Page 9: Java Web Programming [7/9] : Struts2 Basics

9

login.jsp

Page 10: Java Web Programming [7/9] : Struts2 Basics

10

welcome.jsp

Page 11: Java Web Programming [7/9] : Struts2 Basics

11

Step 2: Write Step 2: Write Action classesAction classes

Page 12: Java Web Programming [7/9] : Struts2 Basics

12

Action Class

Extend com.opensymphony.xwork2.ActionSupport class

Decide set of properties that reflect the input form Write getter and setter methods for each property Write execute() method

Page 13: Java Web Programming [7/9] : Struts2 Basics

13

LoginAction.java

Page 14: Java Web Programming [7/9] : Struts2 Basics

14

Step 3: Config Step 3: Config struts.xmlstruts.xml

Page 15: Java Web Programming [7/9] : Struts2 Basics

15

struts.xml Identify required Action's and then define them as

<action> elements

Page 16: Java Web Programming [7/9] : Struts2 Basics

16

Step 5: Build, Deploy,Step 5: Build, Deploy,and Test Application and Test Application

Page 17: Java Web Programming [7/9] : Struts2 Basics

17

Results

Page 18: Java Web Programming [7/9] : Struts2 Basics

18

Acknowledgement

Most contents are borrowed from the presentation slides of Sang Shin, Java™ Technology Evangelist, Sun Microsystems, Inc.

Page 19: Java Web Programming [7/9] : Struts2 Basics

19

Thank you

[email protected]

www.facebook.com/imcinstitute

www.imcinstitute.com