View technologys for generated web layers

myndian.de

Donnerstag, 27. Dezember 2007

View technologys for generated web layers

We use model driven software development in many places in the company. I think, some workmates (consultants ;-) ) do not really know that they only build a model that is generating the real application. But that’s OK. Some colleagues are working on a web application, were the web interface will be generated out of an description in XML. The same applies for the framework I use most the time. As I have a some experience in this area, I tried to tell them what works well and what does not. They asked me initially, but I do not think they wanted to hear the answer I gave them. ;-)

They like to use JSP as the view technology, but the lead architects forbade this, because of testability. So they looked after alternatives, like Velocity and FreeMarker. I told them to have a look at StringTemplate. That’s an amazing template engine. You can even say that it is the only real template engine that exits. There are some scientific paper from Terence Parr - who is professor of computer science at the University of San Francisco - that explain what a template engine should allow you to do and what it should not allow and why StringTemplate fits in this academic definition of template engines.

Look at this quote from the FreeMarker Site:
[...] you separate the designers (HTML authors) from the programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become polluted with complex program fragments. [...] it helps to keep the application clear and easily maintainable.


Does FreeMarker really fits in this description? According to Terence Parr it does not, because FreeMarker has some programming capabilities. “Some” is just too much here. Like with PHP or JSP, which are full blown programming languages you could work that way! But with a real template engine you have to work that way! Why do a HTML designers need to multiply numbers? Why do they need to make database querys? They do not need to. And that’s why nobody should need a template engine with “programming capabilities”. As everybody knows it is not easy to divide the template from the controller or renderer and it is not difficult to mix them together.

How often have you seen code like:
Your order from %<%= new SimpleDateFormat(“yyyy.MM.dd HH:mm:ss”).format(order.getDate()) >
That is to much logic for a template in opinion! Even if you move that into a Tag Lib or a Velocity or FreeMarker Macro like
Your order from #dateFormat(order.Date “yyyy.MM.dd HH:mm:ss”)
you have not really achieved anything in decoupling the template from the controller. Using StringTemplate you could only write
Your order from $order.Date$
and nothing more. That really decouples the template and the controller.

A question to the architects: Would you (unit) test these template? I think you can, because it’s really easy, but you would not, because you want to test the logic and not the template engine.

There is only one thing you could do with StringTemplate that you should not do:
This is <span style=“color:$colors.Red$”>bad</span> usage!


But remember the background: The web layer is generated from a description! Taking this into account you have to ask yourself which of two general ways you want to use. Do you want to generate one complete template for each view or do you want to compose the views out of many fragments?

I think the first one - generating complete pages - may have advantages according performance. Especially if you use JSP which is (according to the german magazine c’t) one of the fastest view technologies available. The greatest disadvantage is that you have to generate all views again and again if you change a template generating those views. Trust me: That’s really annoying. Have you ever spend a hour waiting for the generation of the applications just to determine that you had made a mistake?

So the main advantage of the second way - composing the views - is that you can change a template and the changed one will be used all over the place. It may have a slower performance at runtime, but this can be addressed using caching. You may even not have to generate code when using this solution. Because you can read the XML description and compose the views at runtime. When going that way, StringTemplate will be the best solution available, because of its abilities to reuse templates in a way you can not with Velocity, FreeMarker or JSP. StringTemplate is made to compose views out of fragments.

Last but not least, there may the a way to use both solutions. In a development environment you could use “composing” and you could use these templates to generate the complete templates for the production environment. But I would stick to the second solution.

Just my two cents.
Geschrieben von Jörg in Softwaretechnik um 08:50 | Kommentar (1) | Trackbacks (0)
Tags für diesen Artikel: java, jsp, model driven software development, mvc, strindtemplate, velocity, web development, web-applikationen
Artikel mit ähnlichen Themen:
  • Premature optimization and object orientation
  • POJOs, POGOs und POCOs
  • More about Groovy SQL Maps
  • Make your SQL Maps Groovy!
  • last language war language trolling
  • Dynamic two dimensional array in Java
  • Mozilla Developer Center
  • static vs. dynamic typed configuration
  • notice: JDBC batch updates
  • the framework framework

Trackbacks
Trackback für spezifische URI dieses Eintrags

Keine Trackbacks

Kommentare
Ansicht der Kommentare: (Linear | Verschachtelt)

Being one of the architects in the project with the aforementioned FreeMarker templates: There is one reason for using a templating language with programming capabilities and - surprise - it is the same constantly advocated by scripting fans: Ease of change. We want our system to be easily extendible and customizable by customers and forcing them to write Java code is one of the more complicated ways to do this. Especially since clean decoupling usually requires some complexity which doesn’t faciliate source code understanding.

Thus we in our project regard programmatic templating as a kind of scripting: We provide easily extendible and modifyable base sources which in turn contain a complete view of the domain - and not just some fragments.

While clean decoupling IMHO is to be favoured in standard programming projects it is not something I would use straight away in a base product line that requires extensibility (by customers, not only by us ;-) and modifiyability.
Kommentar (1)
#1 Thomas Biskup (Homepage) am 06.10.2008 08:41 (Antwort)

Kommentar schreiben

Umschließende Sterne heben ein Wort hervor (*wort*), per _wort_ kann ein Wort unterstrichen werden.
Standard-Text Smilies wie :-) und ;-) werden zu Bildern konvertiert.
Die angegebene E-Mail-Adresse wird nicht dargestellt, sondern nur für eventuelle Benachrichtigungen verwendet.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss um dieses Verfahren anzuwenden.
CAPTCHA

Sie können [geshi lang=LANG][/lang] Tags verwenden um Quellcode abhängig von der gewählten Programmiersprache einzubinden
 
 

Suche

Statische Seiten

Startseite
Galerien
Impressum

Kategorien

  • XML Alltag
  • XML Internet
  • XML Musik
  • XML Politik
  • XML Reise
  • XML Softwaretechnik
  • XML Sonstiges
  • XML Visuelles

Alle Kategorien

Archive

Mai 2013
April 2013
März 2013
Das Neueste ...
Älteres ...

Blog abonnieren

XML RSS 2.0 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Kommentare

Login

Verwaltung des Blogs

Login

Aktuelle Einträge

Netzwerkkultur verändert die Gesellschaft
Dienstag, 17. November

Absolute and relative date and time
Sonntag, 18. Oktober

Oren Lavie - Her Morning Elegance
Dienstag, 6. Oktober

Twitter & Blogroll
Samstag, 8. August

Read It Later: Round-Trip-Integration mit Firefox und Google-Reader
Montag, 3. August

Blogroll

* Jörg bei Twitter
* Jens bei Twitter
* Nils bei Twitter

* Beetlebum
* a life less ordinary?
* Martin Fowler's Bliki
* Springify
* BILDblog
* Plazeboalarm
* LawBlog
* ADOM Blog
* Being busy
* Dr. Gero Presser

Links

* Heise
* The Scala Programming Language
Nils' Fotos bei fotocommunity.de
Jogi auf Qype
Get Firefox!
Use OpenOffice.org

Heise News

* Gewinner und Verlierer im Geschäft mit Grafkchips

* Amazons Video-Transcoder mit erweitertem Support

* Erfolgreicher Forschernachwuchs

* Streit um die Kontrolle des Internet

* Bitkom bezeichnet App-Rückgaberecht als realitätsfern

kostenloser Counter