2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal Provide enough understanding for...

52
2008 - Shale and JSF Ken Sipe - Code Mentor

Transcript of 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal Provide enough understanding for...

Page 1: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

2008 - Shale and JSF

Ken Sipe - Code Mentor

Page 2: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

2

Presentation Goal

Provide enough understanding for someone new to JSF and Shale to start coding…

Page 3: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

3

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 4: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

4

About the Speaker

Sun Certified Java 2 Architect. Instructor for VisiBroker for Java, OOAD, Rational Rose,

and Java Development. Frequently speaks on the subject of distributed

computing programming, including CORBA and EJB architecture.

JBoss Certified Developer ;)

Page 5: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

5

What is Shale?

JavaServer Faces JSF++ Extends JSF to add Functionality Added Components without the overhead of committee

Struts 2.0 Well it is the same guys who are involved A number of components carried over

Tiles FileUpload

Doesn’t work in the same MVC model of struts 1.x

http://struts .apache.org/shale http://wiki.apache.org/struts/StrutsShale

Page 6: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

6

Shale Features

View Lifecycle Events Dialog

Web Flow

AJAX Support Validation

Commons Validator

Tiles Integration Spring Integration Tapestry-Like Views

Page 7: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

7

Shale Tag Library

<s:token> Token pattern, used to prevent double posts

<s:subview> Used for Tiles... and dynamically added content

<s:commonsValidator> Used for commons validation

Client and Server

<s:validatorScript> Used with commons validation Client side

<s:clay> Tapestry Like

Page 8: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

8

JSF Extension Points

ShalePropertyResolver Value binding expressions for JNDI

ShaleVariableResolver String Integration JNDI Lookups

ShaleViewHandler Lifecycle events for ViewControllers

TilesViewHandler Maps tile components

DialogNavigatioHandler The space between request and session

Page 9: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

9

Shales Future

Ready a big question Craig says perhaps:

Struts 2.0 New Apache project Part of MyFaces Part of JSF 2.0

Page 10: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

10

Shale Library References

Shale provides a number if jars for distribution: shale-core.jar shale-spring.jar shale-clay.jar shale-test.jar shale-tiles.jar

To use tiles you'll need tiles-core.jar

There are 2 tlds taglib.tld – contained in shale-cores.jar shale-clay.tld – contained in shale-clay.jar

Page 11: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

11

Shale References

Shale makes significant use of jakarta commons commons-logging commons-validator commons-digester commons-beanutils commons-chain commons-collections

Page 12: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

12

JBuilder Shale Configuration

Create a Library with Shale-core.jar Extract taglib.tld Configure a custom tag lib

Page 13: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

13

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 14: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

14

JSF Events

JSF defines standard server side events and listener APIS: ActionEvent calls xxx_action() method and/or event listeners ValueChangeEvent calls xxx_valueChanged() method and/or

event listeners

Sufficient to deal with most low level UI components Insufficient to deal with application level events

Page 15: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

15

Shale View Life-cycle Events

Shale extends server side event paradigm ViewController backing bean per page:

Standard “View Helper” design pattern Supports standard event handler methods init() preprocess() prerender() destroy()

Great for handling PostBacks in a slightly different way than the initial view.

Page 16: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

16

Using ViewController

Implement the ViewController interface. org.apache.shale.view.ViewController

Provide a no-args public constructor. Declare your backing bean as a managed bean.

In nearly all circumstances, you will want the bean to be placed in request scope.

Provide public JavaBean properties that may be set to customize the functionality provided by this bean.

Ensure that the functionality of your backing bean is not dependent upon the order in which property setters are called.

Create event handlers for the lifecycle events of interest, plus standard JavaServer Faces event handlers for things like actions and value changes.

Page 17: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

17

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 18: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

18

JSF Validation

JSF defines standard server validation capabilities which integrates well with JSF components

1..* validators per component are possible Model updates are only after validations occur.

Page 19: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

19

Shale Validation

Jakarta Commons Validator Leverage the same rules created for struts 1.x applications Leverage same rules for other frameworks / applications

integrated with commons

Client-Side Validation Extends past JSF by providing validation at the client side

Tag Support <s:commonsValidator>

Client-side or Server-side or Both

<s:validatorScript> Generators client-side JavaScript

Page 20: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

20

XML Configuration

Although there can be any number of files used for validation configuration, typically there is at least : Validation-rules.xml

Contains the xml defined rules Typically generic / Non application specific Provided with the struts download

Page 21: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

21

Validation-rules.xml

<validator name="required"

classname="org.apache.struts.validator.FieldChecks"

method="validateRequired"

methodParams="java.lang.Object,

org.apache.commons.validator.ValidatorAction,

org.apache.commons.validator.Field,

org.apache.struts.action.ActionErrors,

javax.servlet.http.HttpServletRequest"

msg="errors.required">

<javascript><![CDATA[ …

Page 22: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

22

Shale Validation Components<h:form onsubmit="return validateForm(this);">

… <h:inputText id="username" value="#{user.username}"> <s:commonsValidator type="required“ arg="User Name"

server="false“ client="true"/> </h:inputText> <h:message for="username"/>

<h:outputLabel for="password"> <h:outputText value="password"/> </h:outputLabel> <h:inputSecret id="password" value="#{user.password}"> <f:validateLength minimum="4" /> </h:inputSecret> <h:message for="password"/>… <s:validatorScript functionName="validateForm" /> </h:form>

Page 23: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

DEMOShale Validation

Page 24: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

24

Presentation Agenda

What is Shale? View LifeCycle Events Validation Spring Integration Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 25: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

25

Shale and Spring

DelegatingVariableResolver Access Spring beans with JSF expressions

#{user}

Zero-Configuration Just package shale-spring.jar and spring.jar in the lib of war applicationContext.xml

Page 26: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

26

Presentation Agenda

What is Shale? View LifeCycle Events Validation Spring Integration Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 27: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

27

Tiles Shale Integration

Add to the lib directory of WAR shale-tiles.jar tiles-core.jar

Page 28: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

28

Tiles and Shale

Can be leverage across all of Shale ViewIds of Dialogs Navigation Rule

<navigation-rule>

<navigation-case>

<from-outcome>*</from-outcome>

<to-view-id>login-page-tile</to-view-id>

</navigation-case>

</navigation-rule>

Page 29: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

29

Shale Tiles Layout

<%@ taglib uri="http://jakarta.apache.org/tiles" prefix="tiles" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<table class='<tiles:getAsString name="tileClass"/>'> <tr class='<tiles:getAsString name="rowClass"/>'> <td class='<tiles:getAsString name="dataClass"/>'> <f:subview id="tile"> <tiles:insert attribute='content' flush="false"/> </f:subview> </td> </tr></table>

Page 30: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

30

Tiles.XML file

<!-- LOGIN PAGE TILE -->

<definition name="login-page-tile" extends="header-menu-content-tile">

<put name="header" value="header-tile"/><put name="menu" value="login-menu-tile"/><put name="content" value="/tiles/content/login/content.jsp"/>

</definition

Page 31: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

31

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 32: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

32

Tapestry “Style”

Define a web page without any code Code is tied to page through html tag attributes.

jsfid

Web can be: Html JSP xml

Page 33: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

33

Separation of Page Designer and Developer

Supports 2 Views Design Time (Web Developers)

http://localhost:8082/app/home.html

Run Time http://localhost:8082/app/home.faces

Designer Works with HTML

Developer JSP to include HTML XML to connect it all together

Page 34: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

34

Configuration

clay-config.xml contains the jsfid replacement mappings

view-config.xml contains the component name references from clay-config.xml

shale-clay.jar

Page 35: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

35

Login HTML

<span jsfid="usernamePrompt"> Username </span> <input jsfid="username" type="text" size="15"/> <span jsfid="usernameMessage"> username error message </span> <span jsfid="passwordPrompt"> Password </span> <input jsfid="password" type="password" size="8"/> <span jsfid="passwordMessage"> password error message </span> </td> <input jsfid="login" type="submit" value="Log on"/>

Page 36: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

36

Login JSP

<f:view> <f:loadBundle basename="resources" var="msgs"/> <sh:clay id="loginTemplate" jsfid="/pages/clay-login.html" managedBeanName="loginPage"/></f:view>

Page 37: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

37

JSP Page not required

WEB.XML configuration: <!-- Clay template suffix override, default is .clay --> <context-param> <param-name>clay-template-suffix</param-name>

<param-value>.html</param-value> </context-param>

Page 38: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

38

Clay-Config.XML file <component jsfid="password" extends="inputSecret" id="password"> <attributes> <set name="size" value="3"/> <set name="value" value="#{userContext.password}"/> </attributes> </component>

<component jsfid="passwordMessage" extends="message" allowBody="false"> <attributes> <set name="for" value="password" /> </attributes> </component>

<component jsfid="login" extends="commandButton" id="login"> <attributes> <set name="action" value="#{managed-bean-name.submit}"/> <set name="value" value="Log In" /> </attributes>

Page 39: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

39

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 40: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

40

AJAX – The New Buzz

Asynchronous JavaScript with XML Except it’s not always Asynchronous And It’s not always XML

Server submissions without a page post and refresh Out-side of Shale support I really like DWR

Direct Web Remoting http://getahead.ltd.uk/dwr/

Page 41: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

41

AJAX / JSF Warning…

If you change the state of expected states values held by server… bad things happen Mainly the short-circuits the 2nd or 3rd phase of JSF (just for a

model update and redisplays the page.

Page 42: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

42

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 43: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

43

Shale Dialogs

Modeled after Springs Web Flow Simplified Not complete duplication of Spring

Modeled as States and Transitions Get your STDs out

Page 44: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

44

Dialog State Types

Action Represents a call to a public method, taking no parameters, and

returning a String that will be treated as the logical outcome

View Represents the rendering of a JavaServer Faces view

Subdialog Represents pushing the state of the current dialog onto a stack,

and starting a specified new dialog at its starting state

Exit Terminates the current dialog

Page 45: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

45

Defining Dialogs - Dialog-config.xml

<dialog name="Create Account" start="User Information">

<!-- Transitions applicable to all states --> <transition outcome="cancel" target="Exit"/> <transition outcome="username" target="User Information"/> <transition outcome="phoneNumbers" target="Phone Numbers"/> <transition outcome="address" target="Address"/> <transition outcome="creditCard" target="Credit Card"/> <transition outcome="summary" target="Summary"/>

<!-- User Information -->

<view name="User Information" viewId="/pages/account.jsp"> <transition outcome="next" target="Phone Numbers"/> </view>

Page 46: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

46

Defining Dialogs

<view name="Summary" viewId="/pages/account.jsp">

<transition outcome="previous" target="Credit Card"/> <transition outcome="finish" target="Create User"/>

</view>

<action name="Create User" method="#{accountPage.finish}"></action>

<!-- Exit --><end name="Exit"

viewId="/pages/login.jsp"/>

Page 47: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

47

Working with Dialogs

Enter dialogs through JSF actions

<h:commandButton

value=‘register’

action=“dialog:Create Account” />

DialogNavigationHandler manages dialog flow From Dialog, navigate back to JSF

Page 48: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

48

Multi-Dialog Configuration Files

<context-param> <param-name>

org.apache.shale.dialog.CONFIGURATION </param-name> <param-value>

/WEB-INF/account-dialogs.xml,/WEB-INF/user-dialogs.xml </param-value></context-param>

Page 49: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

49

Presentation Agenda

What is Shale? View LifeCycle Events Validation Tiles Integration Clay – Tapestry-like Views AJAX Dialogs

Page 50: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

50

Summary

Shale makes many common web development task easier.

Extension of JSF Not bound to JCP committee

Page 51: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

Questions?

Page 52: 2008 - Shale and JSF Ken Sipe - Code Mentor. 2 Presentation Goal  Provide enough understanding for someone new to JSF and Shale to start coding…

52

Thank You

2008

Web Application Development using Struts, Shale and JSF

Please fill out the speaker evaluation

You can contact me further at …[email protected]