Top Banner
2007 JavaOne SM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam and Eclipse Gavin King JBoss, A Division of Red Hat [email protected]
77

Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

May 29, 2018

Download

Documents

lethuy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 |

TS-4092

Developing Applications With Seam and Eclipse

Gavin KingJBoss, A Division of Red Hat

[email protected]

Page 2: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 2

My Dilemma

• If I talk about the really unique stuff in Seam, everyone thinks it must be complicated and difficult to use

Page 3: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 3

My Dilemma

• If I talk about the really unique stuff in Seam, everyone thinks it must be complicated and difficult to use

• If I show how easy it is to use, people think it’s just another action/CRUD framework

Page 4: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 4

My Dilemma

• If I talk about the really unique stuff in Seam, everyone thinks it must be complicated and difficult to use

• If I show how easy it is to use, people think it’s just another action/CRUD framework

• Usually I go down the first path, because at least that way the presentation is interesting

Page 5: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 5

My Dilemma

• If I talk about the really unique stuff in Seam, everyone thinks it must be complicated and difficult to use

• If I show how easy it is to use, people think it’s just another action/CRUD framework

• Usually I go down the first path, because at least that way the presentation is interesting

• Today I’m going to try the second path, and risk being boring

Page 6: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 6

My Dilemma

• If I talk about the really unique stuff in Seam, everyone thinks it must be complicated and difficult to use

• If I show how easy it is to use, people think it’s just another action/CRUD framework

• Usually I go down the first path, because at least that way the presentation is interesting

• Today I’m going to try the second path, and risk being boring

• …because I’m sick to death of hearing people who think that Java technology has to be complicated, and has to involve reams and reams of XML files, and has to mean waiting around for the server to restart for hours each day

Page 7: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 7

Getting Started

• After the first day of development, I want to have something to show the users of my system

Page 8: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 8

Getting Started

• After the first day of development, I want to have something to show the users of my system

• Most Java projects spend weeks arguing about frameworks, project structures, architecture, programming in PowerPoint (ugh) or rational rose (worse), writing build scripts, etc.

Page 9: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 9

Getting Started

• After the first day of development, I want to have something to show the users of my system

• Most Java projects spend weeks arguing about frameworks, project structures, architecture, programming in PowerPoint (ugh) or rational rose (worse), writing build scripts, etc.

• The user doesn’t care about this sh*t

Page 10: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 10

Getting Started

• After the first day of development, I want to have something to show the users of my system

• Most Java projects spend weeks arguing about frameworks, project structures, architecture, programming in PowerPoint (ugh) or rational rose (worse), writing build scripts, etc.

• The user doesn’t care about this sh*t• I don’t have to be an obsessive unit-testing-pair-

programming-no-overtime-collective-code-ownership-time-boxed-releases fetishist to think that it’s good to spend time on sh*t that users care about

Page 11: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 11

Getting Started

• seam-gen can get you started in minutes…

Page 12: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 12

What Was Generated?

• A basic project skeleton• With an Ant build script• That deploys to a WAR or exploded directory• With support for persistence via JPA

• With test, dev, and prod database profiles• Basic login/logout• A Facelets template

• What if we don’t like it?• And a welcome page

• What if we don’t like the welcome message?

Page 13: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 13

Let’s Create Our Own Page<ui:composition xmlns="http://www.w3.org/1999/xhtml"

xmlns:ui="http://java.sun.com/jsf/facelets"template="layout/template.xhtml">

<ui:define name="body">Hello!

</ui:define>

</ui:composition>

Page 14: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 14

Now for Some Dynamic Content…<ui:composition xmlns="http://www.w3.org/1999/xhtml"

xmlns:ui="http://java.sun.com/jsf/facelets"template="layout/template.xhtml">

<ui:define name="body">Hello!The time is #{currentTime}.

</ui:define>

</ui:composition>

Page 15: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 15

Using JSF Components<ui:composition xmlns="http://www.w3.org/1999/xhtml"

xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"xmlns:s="http://jboss.com/products/seam/taglib"template="layout/template.xhtml">

<ui:define name="body"><rich:panelBar>

<rich:panelBarItem label="hello">Hello!</rich:panelBarItem><rich:panelBarItem label="time">

The time is &#160;<h:outputText value="#{currentTime}">

<s:convertDateTime type="time" timeStyle="long"/></h:outputText>.

</rich:panelBarItem><rich:panelBarItem label="nothing">nothing to see here</rich:panelBarItem>

</rich:panelBar></ui:define>

</ui:composition>

Page 16: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 16

What Do Actions Look Like?

• It’s time to write some Java code!

Page 17: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 17

What Do Actions Look Like?

• It’s time to write some Java code!• There are no “actions” in Seam

Page 18: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 18

What Do Actions Look Like?

• It’s time to write some Java code!• There are no “actions” in Seam• Or, if you prefer, all Seam components

are “actions”• Seam-gen pretends that there is something called

an “action”, just to make it easy for Struts and Ruby on Rails guys

Page 19: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 19

What Do Actions Look Like?

• It’s time to write some Java code!• There are no “actions” in Seam• Or, if you prefer, all Seam components

are “actions”• Seam-gen pretends that there is something called

an “action”, just to make it easy for Struts and Ruby on Rails guys

• A traditional action framework binds a class to a URL, and request parameters to attributes, unidirectionally—JavaServer™ Faces lets you bind buttons to methods and fields to attributes, bidirectionally• Let’s make an “action” now…

Page 20: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 20

What Was Generated?<ui:composition xmlns="http://www.w3.org/1999/xhtml"

...template="layout/template.xhtml">

<ui:define name="body">

<h:messages globalOnly="true" styleClass="message"/>

<rich:panel label="ping">

<h:form id="pingForm"><h:commandButton id="ping" value="ping!"

action="#{ping.ping}"/></h:form>

</rich:panel>

</ui:define>

</ui:composition>

Page 21: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 21

What Was Generated?@Name("ping")public class Ping {

@Logger private Log log;

@In FacesMessages facesMessages;

public void ping(){

log.info("ping.ping() action called");facesMessages.add("ping");

}

}

Page 22: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 22

What Was Generated?public class PingTest extends SeamTest {

@Testpublic void test() throws Exception {

new FacesRequest() {@Overrideprotected void invokeApplication() {

invokeMethod("#{ping.ping}");}

}.run();

}}

Page 23: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 23

Not a Unit Test!

• Unit tests test a component in isolation (supposedly)

Page 24: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 24

Not a Unit Test!

• Unit tests test a component in isolation (supposedly)

• Hence they are next to useless

Page 25: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 25

Not a Unit Test!

• Unit tests test a component in isolation (supposedly)

• Hence they are next to useless• Useful tests test the interaction between a

component and its collaborators and container environment• Please, please stop writing StringUtilTest classes

Page 26: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 26

Not a Unit Test!

• Unit tests test a component in isolation (supposedly)

• Hence they are next to useless• Useful tests test the interaction between a

component and its collaborators and container environment• Please, please stop writing StringUtilTest classes

• Or they test that the external behavior of the system is correct, from the point of view of the user

Page 27: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 27

Debugging

• Debugging is easy!• Let’s try debugging the test case…• Let’s try debugging the running app…• Now, let’s meet the Seam debug page…

Page 28: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 28

Using Seam Security@Name("ping")public class Ping {

@Logger private Log log;

@In FacesMessages facesMessages;

@Restrict("#{identity.loggedIn}")public void ping(){

log.info("ping.ping() action called");facesMessages.add("pong!");

}

}

Page 29: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 29

Using EL in the Action@Name("ping")public class Ping {

@Logger private Log log;

@In FacesMessages facesMessages;

@Restrict("#{identity.loggedIn}")public void ping(){

log.info("ping.ping() action called by #{identity.username}");facesMessages.add("pong to #{identity.username}");

}

}

Page 30: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 30

Calling the Action by Ajax<ui:composition xmlns="http://www.w3.org/1999/xhtml"

...template="layout/template.xhtml">

<ui:define name="body">

<s:span id="msg"><h:messages globalOnly="true" styleClass="message"/>

</s:span>

<rich:panel><f:facet name="header">ping</f:facet>

<h:form id="pingForm">

<a:commandButton id="ping" value="ping!" action="#{ping.ping}" reRender="msg"/>

</h:form>

</rich:panel>

</ui:define>

</ui:composition>

Page 31: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 31

Drag and Drop<s:span id="msg">

<h:messages id="msg" globalOnly="true" styleClass="message"/></s:span>

<rich:panel label="ping">

<h:form id="pingForm">

<rich:panel><rich:dragSupport dragType="text"/>drag me!

</rich:panel>

<rich:panel><rich:dropSupport id="drop" acceptedTypes="text"

dropListener="#{ping.ping}" reRender="msg"/>drop me!

</rich:panel>

...</h:form>

</rich:panel>

Page 32: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 32

Navigation From an Action@Name("ping")public class Ping {

@Logger private Log log;

@In FacesMessages facesMessages;

@Restrict("#{identity.loggedIn}")public String ping(){

log.info("ping.ping() action called by #{identity.username}");facesMessages.add("pong to #{identity.username}");return "/home.xhtml";

}

}

Page 33: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 33

Using #xternal Navigation Rules<!DOCTYPE page PUBLIC

"-//JBoss/Seam Pages Configuration DTD 1.2//EN""http://jboss.com/products/seam/pages-1.2.dtd">

<page><navigation from-action="#{ping.ping}">

<redirect view-id="/home.xhtml"/></navigation>

</page>

Page 34: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 34

“One Kind of Stuff”

• A single component model that unifies JavaServer Faces, Enterprise JavaBeans™ (EJB™) 3, BPM, WS, ESB…

• A single rendering technology for HTML, XML, email, PDF…

• A single EL for binding rendering, orchestration, configuration to the component model

Page 35: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 35

Sending an Email<m:message xmlns="http://www.w3.org/1999/xhtml"

xmlns:m="http://jboss.com/products/seam/mail">

<m:from name="Seam" address="[email protected]" /><m:to name="#{identity.username}">#{identity.username}@hibernate.org</m:to><m:subject>Hello...</m:subject><m:body>

Hello #{identity.username}!</m:body>

</m:message>

Page 36: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 36

Sending an Email@Name("ping")public class Ping {

@Logger private Log log;

@In FacesMessages facesMessages;@In Renderer renderer;

@Restrict("#{identity.loggedIn}")public void ping(){

log.info("ping.ping() action called by #{identity.username}");facesMessages.add("pong to #{identity.username}");renderer.render("/email.xhtml");

}

}

Page 37: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 37

What About Persistence?

• Remember how we said there are no actions in Seam?

Page 38: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 38

What About Persistence?

• Remember how we said there are no actions in Seam?

• There are no DAOs either• At least not in the traditional sense

Page 39: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 39

What About Persistence?

• Remember how we said there are no actions in Seam?

• There are no DAOs either• At least not in the traditional sense

• In Seam it is common to bind components which access the database via JPA/Hibernate—and the entities themselves—directly to the view

Page 40: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 40

What About Persistence?• Remember how we said there are no actions in Seam?• There are no DAOs either

• At least not in the traditional sense

• In Seam it is common to bind components which access the database via JPA/Hibernate—and the entities themselves—directly to the view

• Seam solves a bunch of persistence-related problems (LazyInitializationException, etc.) by providing persistence context management and transaction management that is aware of the boundaries of the user interaction • A conversation-scoped persistence context• We havn’t met conversations yet, so let’s start with something simple…

Page 41: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 41

The Entity Bean@Entitypublic class User {

@Idprivate String username;private String name;

public String getName() {return name;

}public void setName(String name) {

this.name = name;}public String getUsername() {

return username;}public void setUsername(String username) {

this.username = username;}

}

Page 42: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 42

Using a Seam Persistence Context

@Name("ping")public class Ping {

@Logger private Log log;

@In FacesMessages facesMessages;

@In EntityManager entityManager;

@Restrict("#{identity.loggedIn}")public void ping(){

User user = (User) entityManager.createQuery("from User where username = #{identity.username}").getSingleResult();

log.info("ping.ping() action called by #{identity.username}");facesMessages.add("pong to #0", user.getName());

}

}

Page 43: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 43

What About Form Processing?

• This is the one thing JavaServer Faces does really well!• Of course, we’ll use seam-gen to get us started…

Page 44: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 44

What Was Generated?<h:form id="adderForm">

<rich:panel><f:facet name="header">adder</f:facet>

<s:decorate id="valueDecoration" template="layout/edit.xhtml"><ui:define name="label">Value</ui:define><h:inputText id="value" required="true"

value="#{adder.value}"/></s:decorate>

<div style="clear:both"/>

</rich:panel>

<div class="actionButtons"><h:commandButton id="adder" value="adder"

action="#{adder.adder}"/></div>

</h:form>

Page 45: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 45

What Was Generated?@Name("adder")public class Adder {

@Logger private Log log;

@In FacesMessages facesMessages;

private String value;

public void add(){

log.info("adder.add() action called with: #{adder.value}");facesMessages.add("add #{adder.value}");

}

//get/set pair for value...

}

Page 46: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 46

What Was Generated?public class AdderTest extends SeamTest {

@Testpublic void test() throws Exception {

new FacesRequest() {@Overrideprotected void updateModelValues() throws Exception {setValue("#{adder.value}", "seam");

}@Overrideprotected void invokeApplication() {

invokeMethod("#{adder.add}");}@Overrideprotected void renderResponse() {assert getValue("#{adder.value}").equals("seam");}

}.run();}

}

Page 47: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 47

Implementing an “adder”@Name("adder")public class Adder {

@Logger private Log log;

@In FacesMessages facesMessages;

private BigDecimal value;private BigDecimal total = BigDecimal.ZERO;

public void add(){

total = total.add(value);log.info("adder.add() action called with: #{adder.value}");facesMessages.add("total: #{adder.total}");

}

//get/set pair for value and total...

}

Page 48: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 48

It Doesn’t Work!

• Our component doesn’t “remember” the total across multiple requests

Page 49: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 49

It Doesn’t Work!

• Our component doesn’t “remember” the total across multiple requests

• We could keep state in the http session• But what if we wanted to be keeping multiple totals

at the same time?

Page 50: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 50

It Doesn’t Work!

• Our component doesn’t “remember” the total across multiple requests

• We could keep state in the http session• But what if we wanted to be keeping multiple totals

at the same time?• Let’s try using a conversation

• Finally, this is our first baby-step into the world of Seam…

Page 51: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 51

Implementing an “adder”@Name("adder")@Scope(ScopeType.CONVERSATION)public class Adder {

@Logger private Log log;

@In FacesMessages facesMessages;

private BigDecimal value;private BigDecimal total = BigDecimal.ZERO;

@Begin(join=true)public void add(){

total = total.add(value);log.info("adder.add() action called with: #{adder.value}");facesMessages.add("total: #{adder.total}");

}

//get/set pair for value and total...

}

Page 52: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 52

Validation

• The word “validation” implies a process that belongs in the user interface

Page 53: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 53

Validation

• The word “validation” implies a process that belongs in the user interface

• But most “validation” enforces constraints that exist all the way to the database• It is an aspect of the data model

Page 54: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 54

Validation

• The word “validation” implies a process that belongs in the user interface

• But most “validation” enforces constraints that exist all the way to the database• It is an aspect of the data model

• Hibernate Validator provides model-based constraints• These are enforced by Seam at the user interface• And by Hibernate, before writing to the database• You can even ask Hibernate to generate DDL that includes

the constraint

Page 55: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 55

Validation

• The word “validation” implies a process that belongs in the user interface

• But most “validation” enforces constraints that exist all the way to the database• It is an aspect of the data model

• Hibernate Validator provides model-based constraints• These are enforced by Seam at the user interface• And by Hibernate, before writing to the database• You can even ask Hibernate to generate DDL that includes

the constraint• There are some nice built-in constraints

• But we want higher-level semantics for our constraints…

Page 56: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 56

Adding Validation—The Model

@Name("adder")@Scope(ScopeType.CONVERSATION)public class Adder {

...

@Moneypublic BigDecimal getValue(){

return value;}

}

Page 57: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 57

Adding Validation—The Annotation

@Retention(RetentionPolicy.RUNTIME)@ValidatorClass(MoneyValidator.class)public @interface Money {

String message() default "please enter an amount in dollars";}

Page 58: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 58

Adding Validation—The Validator

public class MoneyValidator implements Validator<Money> {

public void initialize(Money money) {}

public boolean isValid(Object value) {

return ((BigDecimal) value).scale() <= 2;}

}

Page 59: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 59

Adding Validation—Customizing the Message

@Name("adder")@Scope(ScopeType.CONVERSATION)public class Adder {

...

@Money(message="you can only add dollar amounts")public BigDecimal getValue(){

return value;}

}

Page 60: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 60

Adding Interactive Validation<s:div id="total">Total: #{adder.total}</s:div>

<h:form id="adderForm"><rich:panel>

<f:facet name="header">adder</f:facet>

<s:decorate id="valueDecoration" template="layout/edit.xhtml"><ui:define name="label">Value</ui:define><h:inputText id="value" required="true"

value="#{adder.value}"><a:support event="onblur" reRender="valueDecoration"/>

</h:inputText></s:decorate>

<div style="clear:both"/>

</rich:panel>

<div class="actionButtons"><a:commandButton id="adder" value="adder"

action="#{adder.add}" reRender="total,valueDecoration"/></div>

</h:form>

Page 61: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 61

Why Ajax Needs Conversations• Ajax changes the interaction model of the web

Page 62: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 62

Why Ajax Needs Conversations• Ajax changes the interaction model of the web• From few, coarse-grained, requests

Page 63: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 63

Why Ajax Needs Conversations• Ajax changes the interaction model of the web• From few, coarse-grained, requests• To many, fine-grained, requests

Page 64: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 64

Why Ajax Needs Conversations• Ajax changes the interaction model of the web• From few, coarse-grained, requests• To many, fine-grained, requests• This is going to kill your database if you don’t have

somewhere to keep conversational state• You can keep some state in the client, but there is plenty you can’t

keep there• For example, you don’t want to serialize persistence contexts back

and forth, if you can possibly avoid it

Page 65: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 65

Why Ajax Needs Conversations• Ajax changes the interaction model of the web• From few, coarse-grained, requests• To many, fine-grained, requests• This is going to kill your database if you don’t have

somewhere to keep conversational state• You can keep some state in the client, but there is plenty you can’t

keep there• For example, you don’t want to serialize persistence contexts back

and forth, if you can possibly avoid it• With asynchronicity comes concurrency

• The servlet engine offers no construct for managing concurrent access to state held in the web tier

• Seam solves this problem by serializing requests for a particular conversation, and by serializing access to session-scoped components

Page 66: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 66

Conversations and Bookmarks

• Conversations are a fantastic feature, but they are all about server-side state

Page 67: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 67

Conversations and Bookmarks

• Conversations are a fantastic feature, but they are all about server-side state

• Usually, this is the most efficient place to keep state• “Shared nothing” architectures simply don’t scale, no matter

how long you all spend sitting around in circles telling each other that they do

Page 68: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 68

Conversations and Bookmarks

• Conversations are a fantastic feature, but they are all about server-side state

• Usually, this is the most efficient place to keep state• “Shared nothing” architectures simply don’t scale, no matter

how long you all spend sitting around in circles telling each other that they do

• The disadvantage of keeping state on the server is that you can’t bookmark it, or send a link in an email

Page 69: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 69

Conversations and Bookmarks

• Conversations are a fantastic feature, but they are all about server-side state

• Usually, this is the most efficient place to keep state• “Shared nothing” architectures simply don’t scale, no matter

how long you all spend sitting around in circles telling each other that they do

• The disadvantage of keeping state on the server is that you can’t bookmark it, or send a link in an email

• Seam provides page parameters, a bidirectional binding from request parameters to the model• So they are automagically propagated across links, redirects, etc.• And the model doesn’t need to know anything about them

Page 70: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 70

Using Page Parameters<!DOCTYPE page PUBLIC

"-//JBoss/Seam Pages Configuration DTD 1.2//EN""http://jboss.com/products/seam/pages-1.2.dtd">

<page><param name="total" value="#{badder.total}"/><navigation from-action="#{badder.add}">

<redirect/></navigation>

</page>

Page 71: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 71

Finale

• The common case in enterprise development is that we have an existing database• If we do, seam-gen can give you basic CRUD,

virtually for free…

Page 72: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 72

What Was Generated?@Entity@Table(name = "User")public class User implements java.io.Serializable {

private String username;private String name;

//constructors...

@Id@Column(name = "username", unique = true, nullable = false)@NotNullpublic String getUsername() {

return this.username;}

public void setUsername(String username) {this.username = username;

}

@Column(name = "name")public String getName() {

return this.name;}

public void setName(String name) {this.name = name;

}

}

Page 73: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 73

What Was Generated?@Name("userHome")public class UserHome extends EntityHome<User> {

public void setUserUsername(String id) {setId(id);

}

public String getUserUsername() {return (String) getId();

}

@Overrideprotected User createInstance() {

User user = new User();return user;

}

public void wire() {}

public boolean isWired() {return true;

}

public User getDefinedInstance() {return isIdDefined() ? getInstance() : null;

}

}

Page 74: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 74

What Was Generated?@Name("userList")public class UserList extends EntityQuery {

private static final String[] RESTRICTIONS = {"lower(user.username) like concat(lower(#{userList.user.username}),'%')","lower(user.name) like concat(lower(#{userList.user.name}),'%')",};

private User user = new User();

@Overridepublic String getEjbql() {

return "select user from User user";}

@Overridepublic Integer getMaxResults() {

return 25;}

public User getUser() {return user;

}

@Overridepublic List<String> getRestrictions() {

return Arrays.asList(RESTRICTIONS);}

}

Page 75: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 | 75

What Was Generated?

• Search, view, and edit pages• With association management• With pagination and sorting• Bookmarkable search results• With interactive validation via Ajax• Conversational editing (easy optimistic locking)

Page 76: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

762007 JavaOneSM Conference | Session XXXX |

Q&A

Page 77: Developing Applications With Seam and Eclipsedocs.huihoo.com/javaone/2007/java-ee/TS-4092.pdf2007 JavaOneSM Conference | Session TS-4092 | TS-4092 Developing Applications With Seam

2007 JavaOneSM Conference | Session TS-4092 |

TS-4092

Developing Applications With Seam and EclipseGavin KingJBoss, A Division of Red Hat

[email protected]