ajax ons2

22
Ajax on Struts 2

Upload: chad-davis

Post on 16-May-2015

401 views

Category:

Technology


0 download

DESCRIPTION

Ajax on Struts 2

TRANSCRIPT

Page 1: Ajax ons2

Ajax on Struts 2

Page 2: Ajax ons2

About Myself

Chad Davis Blackdog Software, Inc. J2EE Consulting Corporate Training Struts 2 in Action Open Source Enthusiast Debian Devotee

Page 3: Ajax ons2

Road Map

Something for everyone Struts 2 introduction Ajax introduction Walk through a code sample Questions at any time

Page 4: Ajax ons2

Struts 2

Web application framework Java Servlets Second generation Software engineering

Page 5: Ajax ons2

Classic versus Ajax

Classic Web Applications

Ajax Web Applications

Page 6: Ajax ons2

Classic Web Applications

URL: /manningSampleApp/chapterEight/ClassicRetrieveUser.actionData: username=mary

<html> <head> <link rel ="stylesheet" type="text/css" href="css/ajaxUserBrowser.css" /> </head> <body> <h2>Artist Browser Control</h2> <form name="ClassicRetrieveUser" action="ClassicRetrieveUser.action"

method ="post"> <select name="username" id="ClassicRetrieveUser_username" > <option value="Jimmy">Jimmy</option> <option value="Charlie Joe">Charlie Joe</option > <option value="Mary" selected="selected" >Mary</option > <option value="Arty" >Arty</option> </form> </body></html>

Page 7: Ajax ons2

How it works: classic style

Browser makes request Http URL Data

Server Processes data Sends HTML page response

Browser receives, renders HTML

Page 8: Ajax ons2

<html><head><link rel="stylesheet" type="text/css" href="css/classicUserBrowser.css" /></head><body><h2>Artist Browser Control</h2><form id="ClassicRetrieveUser" name="ClassicRetrieveUser" onsubmit="return true;" action="/manningSampleApp/chapterEight/ClassicRetrieveUser.action" method="post"><table class="wwFormTable"> <tr> <td class="tdLabel"><label for="ClassicRetrieveUser_username" class="label">Select an artist:</label></td> <td><select name="username" id="ClassicRetrieveUser_username"> <option value="Jimmy">Jimmy</option> <option value="Charlie Joe">Charlie Joe</option> <option value="Mary" selected="selected">Mary</option> <option value="Arty">Arty</option> </select></td> </tr> <tr> <td colspan="2"> <div align="right"><input type="submit" id="ClassicRetrieveUser_0" value="Submit" /></div> </td> </tr></table></form><hr/><h2>Artist Information</h2><div id='console'><p><span class="browser_label">Name:</span> Mary Greene</p><p><span class="browser_label">PortfolioName: </span>Wood Cuts</p><p><span class="browser_label">PortfolioName: </span>Oil Paintings</p></div></body></html>

Page 9: Ajax ons2

Classic Problems

Slow High bandwidth Redundant Page rendering

Page 10: Ajax ons2

Ajax Web Applications

URL: /manningSampleApp/chapterEight/ClassicRetrieveUser.actionData: username=mary

{"artist":{

"username":"Mary","password":"max","portfolioName":"Mary's Portfolio","birthday":"2008-10-29 15:10:25.857MDT","firstName":"Mary","lastName":"Greene","receiveJunkMail":"false",

}}

Page 11: Ajax ons2

How it works: Ajax

Browser uses Javascript to submit request Http URL Data

Server Processes data Sends data response ( JSON, XML, etc. )

Browser Javascript Proceses data DHTML

Page 12: Ajax ons2

{"artist":{

"username":"Mary","password":"max","portfolioName":"Mary's Portfolio","birthday":"2008-10-29 15:10:25.857MDT","firstName":"Mary","lastName":"Greene","receiveJunkMail":"false","portfolios":{"entry":[ {"string":"Wood Cuts", "manning.chapterEight.utils.Portfolio":{"name":"WoodCuts"}}, {"string":"Oil Paintings", manning.chapterEight.utils.Portfolio":{"name":"Oil Paintings"}}]}

}}

Page 13: Ajax ons2

Ajax Selling Points

Low bandwith No page rendering issues Supports a stronger Separation of Concerns

Page 14: Ajax ons2

Observations . . .

What does the server do?

The page abstraction: JSP, ASP, PHP

What should new frameworks do?

What should new frameworks do?

Page 15: Ajax ons2

Struts 2 Architecture

Does all the dirty work for you Separation of Concerns !! Interceptors, Actions, Results, ValueStack

Page 16: Ajax ons2
Page 17: Ajax ons2

Daily development

Actions you write them

Results declare them write them if necessary

Interceptors nothing! declare or write if necessary

Page 18: Ajax ons2

Configuration

Declare your actions Declare your results XML, Annotations

Page 19: Ajax ons2

struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<package name="chapterEightPublic" namespace="/chapterEight" extends="struts-default">

<action name="ClassicUserBrowser" class="manning.chapterEight.UserBrowser"><result>classicUserBrowser.jsp</result>

</action>

</package>

</struts>

<struts>

Page 20: Ajax ons2

Let's code: classic style

What do we need to write?Interceptors?

Results?An Action

A JSP Page

Page 21: Ajax ons2

Let's Code: Ajax Style

What do we need to write?Interceptors?

Results?An ActionA JSP Page?Javascript Client Application

Page 22: Ajax ons2

Summary

Struts 2 – Second Generation Framework

Struts 2 – Built on Software Engineering Principles

Struts 2 – Fast Development, Flexible Architecture

Ajax – No Page Rendering

Ajax – Javascript Client

Ajax – Lower Bandwidth

Ajax – JSON, XML