44
Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Embed Size (px)

Citation preview

Page 1: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt1

Velocity Workshop

Templatesprache imTurbine-Kontext

Page 2: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt2

Was ist Velocity?

• Java-basierte Template-Engine

• Verwendung für– Code-Generierung– Email-Templating– Web-UI-Erzeugung

Im Zusammenspiel mit Turbine:

Fokus auf Web-UI-Erzeugung

Page 3: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt3

Wie funktioniert Templating?

VelocityVelocity

Page 4: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt4

Modell-View-Controller

VelocityVelocity

Page 5: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt5

Vorteile des Templating

• Trennung von Content / Layout

dadurch

• Trennung von Programmierer / Designer

• Referenzierung von Methoden, die in Java geschrieben sind (Velocity)

Page 6: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt6

Velocity und Turbine

• Velocity verwendet Referenzen aus dem Turbine-Context

• Velocity repräsentiert den View-Teil

• Turbine repräsentiert den Modell & Controller-Teil

Page 7: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt7

SeitenaufbauFührt evt. notwendigeActions aus.

Physikalische Strukturder zu erzeugenden Web-Site

Stellt den eigentlichen Körperder Web-Site dar. Verantwortlich für Benutzerinteraktion und Datenanzeige.

Page 8: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt8

AufrufreihenfolgeAufruf mit

http://..../HelloWorld.vm

Page 9: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt9

Sprachkonzepte

Page 10: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt10

Velocity Template Language (VTL)

• Kommentare

• Variablen/Methoden

• Wichtige Befehle

Page 11: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt11

Kommentare

• Kommentarzeile

## Das ist ein Kommentar!

• Kommentarblock#*

Das ist ein Kommentarblock!

*#

Page 12: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt12

Variablen

• Notation: $ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]

• Beispiele:– $count-x– $!count-x– ${count-x}– \$count-x

Page 13: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt13

Methoden

• Aufruf einer Methode

$kunde.getKundennr()

• Kurze Schreibweise bei get/set Methoden

$kunde.Kundennr

Page 14: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt14

Wichtige Befehle

#set

#if

#foreach

#stop

#include

#parse

#macro

Page 15: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt15

#set (1)

• Notation:#set( $ref = [",‘]arg[",‘] )

• Beispiele:#set( $name = "Markus„ )

#set( $test.Feld = ["E1","E2"] )

#set( $test.Feld2 = [1..10])

#set( $test.Map = {"schlüssel1":"wert1","s2":"w2"} )(ab Velocity v1.4)

Page 16: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt16

#set (2)

• Ist der rechte Ausdruck einer #set Anweisung leer, wird NICHT null zugewiesen!!

• Text zwischen einfachen Anführungszeichen wird NICHT geparsed!Kann in der Datei velocity.properties umgestellt werden.

Page 17: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt17

#if

#if ( $Name == "Franz" || $Name == "Huber" )

Huber Franz

#elseif

unbekannt

#end

• Logische OperatorenNOT -> !

AND -> &&

OR -> ||

Page 18: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt18

#foreach

#foreach( $product in $allProducts )

<li>$product</li>

#end

• Verfügbarer Schleifenzähler $velocityCount• Beginnt standardmäßig bei 1, dies kann jedoch in der

Datei velocity.properties auf 0 geändert werden.

Page 19: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt19

#stop

• Beendet die Übersetzung eines Velocity Templates

• Dies kann beim Debuggen sinnvoll sein.

Page 20: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt20

#include

• Notation:#include( arg[, arg2, … argn] )

• Beispiel:#include ( "lieferantenliste.txt", $kundenliste )

• Die eingefügten Dateien werden NICHT übersetzt.

Page 21: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt21

#parse

• Notation:#parse( arg )

• Beispiel:#parse ( "kundenausgabe.vm")

• Die eingefügten Dateien werden übersetzt.• Es kann nur eine Datei auf einmal eingefügt werden!

Page 22: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt22

#macro (1)

• Notation:#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] )

[ VM VTL code... ]

#end

• Beispiel

#macro ( listenausgabe $liste )

#foreach $elem in $liste

<tr><td>$elem</td></tr>

#end

#end

Page 23: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt23

#macro (2)

• Anwendung#set ( $liste = [‘‘Zeile 1",‘‘Zeile 2"] )

<table>

#listenausgabe ( $liste )

</table>

• Übergibt man einem Makro als Parameter eine Methode, so wird diese bei jedem Zugriff ausgeführt.

Page 24: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt24

Velocity im Turbine Context

Page 25: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt25

Velocity und Turbine Context

• Verknüpfung zwischen Javacode und Template

• Verwaltung in einer Hashtable• Objekte:

– RunData– TemplateLink– TemplatePageAttributes

• Manuelles Hinzufügen von Objekten möglich

Page 26: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt26

Verwendung RunData (1)

• Referenzierung im Velocity mit $data

• Alle public Methoden des Objekts verfügbar

• Sehr große Anzahl an Methoden Gesamtliste unter Turbine Javadocs

• Nachfolgend einige wichtige aufgezählt

Page 27: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt27

Verwendung RunData (2)

• getMessage()

• addMessage(String msg)

• getTitle()

• getUser()

• getServerName()

• setRedirectUri(String ruri)

• getTemplateInfo()

Page 28: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt28

Verwendung TemplateLink (1)

• Referenzierung im Velocity mit $link

• Klasse TemplateLink eine Erweiterung der Klasse DynamicURI

• Verwendung: Erzeugung von URIs

• Bspw. Zu einem anderen Template:$link.setPage(„newpage.vm“)

Page 29: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt29

Verwendung TemplateLink (2)

• setPage(String templatename)

• addPathInfo(String name, String value)

• addQueryData(String name, String value)

• getScriptName()

• getServerData()

• setAction(String action)

• setScreen(String screen)

• setSecure()

Page 30: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt30

Verwendung TemplatePageAttributes (1)

• Referenzierung im Velocity mit $page

• Damit können bestimmte HTML – Eigenschaften gesetzt werden– Hintergrundfarbe– Titel– Textfarbe– Usw.

Page 31: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt31

Verwendung TemplatePageAttributes (2)

• addAttribute(String name, String value)

• getTitle()

• setBackground(String url)

• setBgColor(String color)

• setTextColor(String color)

• setKeywords(String description)

• setLinkColor(String color)

Page 32: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt32

Eigene Objekte im Turbine Context (1)

• Context wird mit Hashtable verwaltet

• Objekte beliebigen Typs können hinzugefügt werden

• Danach alle public Methoden des Objekts im Template (Velocity) verfügbar

• Hinzufügen von Objekten mit dem Wert NULL NullPointerException

Page 33: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt33

Eigene Objekte im Turbine Context (2)

• Beispiel String-Objekt

• Im Javacode:– context.put(„hello“,new String(„testing“))

• Im Template:– $hello the hello reference

• Ergibt als Output:– testing the hello reference

Page 34: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt34

Actions

Page 35: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt35

Actions

Aufruf mit HelloWorld.vm/action/UpdateWorldHelloWorld.vm/action/UpdateWorld_MethodName

• Ruft das Template HelloWorld.vm auf

• Vorher wird die VelocityAction UpdateWorld ausgeführt

Page 36: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt36

Velocity Einstellungen

Page 37: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt37

Konfiguration

• richtige Ordnerstruktur

• Einstellungen in der TurbineRessources.properties

Page 38: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt38

Turbine Ordnerstruktur

Page 39: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt39

templates Ordner

• Enthält alle Velocity Templates (.vm)

• app - GlobalMacros.vm

• layouts - übergeordnete Layout

• navigation – Navigationselemente

• screens – eigentlichen Inhalte

Page 40: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt40

src Ordner

• spiegelt Struktur im class Ordner wieder

• modules – zu *.vm zugehöriger Java Code

• screens – eigentliche Inhaltssteuerung

• actions – spezielle Aktionen

Page 41: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt41

Einstellungen

• unter WEB-INF\conf\

• in der (Text-)Datei TurbineRessources.properties

Page 42: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt42

Einstellungen 1/2

F R A M E W O R K S E T T I N G S

• template.homepage=Index.vm• template.login=Login.vm• template.error=Error.vm

T E M P L A T E S E R V I C E

• services.TemplateService.layout.cache.size=2• services.TemplateService.navigation.cache.size=10• services.TemplateService.screen.cache.size=50

Page 43: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt43

Einstellungen 2/2V E L O C I T Y S E R V I C E• services.VelocityService.default.layout.template = Default.vm• services.VelocityService.velocimacro.library = GlobalMacros.vm• services.VelocityService.file.resource.loader.path =

/templates/app

C A C H I N G• services.VelocityService.file.resource.loader.cache = false• services.VelocityService.file.resource.loader.modification

CheckInterval = 2• services.VelocityService.velocimacro.library.autoreload = true

Page 44: Velocity Workshop SE Projekt 1 Velocity Workshop Templatesprache im Turbine-Kontext

Velocity WorkshopS

E P

roje

kt44

Eclipse: Velocity Plugin

• unter: http://veloedit.sourceforge.net

• Unterstützt beim Bearbeiten von .vm Dateien– Syntax Highlighting– Source Insight (auch bei eigenen Makros)– Strukturansicht– ...