0

I've done some php dev and the big trend in this language is using things like smarty or other template engine.

It usually roughly runs as follows :

load the template as a regular string, look for its {tags} replace each {tag} with the result of some code. cache page with input parameters render resulting page. (sometimes add some OO principles such as template becomes an object...)

When I look at jsp, I see usage of scriplets, taglibs with complicated things like

<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ page import="ghhghjjgj"%>

then :

<logic:if>some html </logic:if>

or worse :

<%= if (blabal) {%>
some html
<%}else ...%>

and so forth.

Okay, tiles enables me to glue together some jsp pages together which is really handy (like the include in php, sort of)

It seems to me that the php approach is much better in the way that :

  • It totally separates gui and model processing.
    -It's easier to change the pages content when you are working on the behind part, you're in a real java class with no complicated stuff like % @ <%=. (who said code behind ala C# ;) ?)

The C# approah is very interesting as well but I just want to adress the template part in my question and not start any C# Vs Java Troll war. I do not also want to say php is better.

I just want to know why there is not a well installed templating engine in java and why we still use scriplets/taglibs.

So I guess I must be missing something. Can some Java EE Web expert show me the flaws of my reasoning?

1
  • Looks like you've got the right idea of indenting your code by 4 spaces. However, you also need to leave a blank line between your code and the paragraph above it for it to be properly recognized as code. Jun 20, 2012 at 9:33

3 Answers 3

3

J2EE became Java EE a long time ago. Drop the "2".

No one should use scriptlets. It's 1999 technology. If you're seeing it in books, it's because the books are old. There's not a lot of good reasons for writing another servlet/JSP book now.

Custom tag libraries have fallen out of favor. JSTL is the standard. It's unlikely that you'll need more than that.

Templating is common - have a look at Velocity. One project I'm working on uses it exclusively for streaming HTML to the browser.

1
  • Edited for clarity. I should have said "custom" tag libraries.
    – duffymo
    Jun 20, 2012 at 9:34
0

There are many template engines for Java, Velocity, for example. JSP compiles to Java bytecode. It allows for very fast execution. Whether this factor is important for you or not, depends on your task, for most web sites template processing won't be an issue.

1
  • Especially since JSPs can be precompiled. No need to pay the first-use penalty.
    – duffymo
    Jun 20, 2012 at 9:50
0

I don't really understand the implications why it's great to write

{if blabal} some html{/if} 

and not so good to write

<logic:if test="blabal"> some html</logic:if>

and it's worse to write

<% if (blabal) { %> some html <% } %>

but it's good to write

#if ( blabal )
    some html
#end

I personally like to write my logic in java.

  • Type save
  • I know the syntax

However, to me it's not a matter of syntax wheter mixing template code and logic is a good or a bad thing. Thus I prefer Snippetory. I gets the logic out of the template while keeping the responsibility for consistence (ecaping stuff and the like), the look (formatting...) and internationalization in the template. The binding logic gets testable, easy to organize and reuse. The data model can be used as is and there's no need to translate it into a model sufficient for some kind of alien technology. In this case the template is rather a kind of a model where you copy the necessary data into rather than a process that self-services from a context.

Now, in this case we'll need to peaces of software to express the same thing, as it always happens as one uses the principles of separation of concerns to get software more maintainable.

Template:

<t:named-region> some html with a {v:value} </t:named-region>

Logic:

if (blabal) {
  template.get("named-region").set("value", value).render();
}

Now as we look at this, it's quite a bit more code. Again this is typical to serparation of concerns. However, a quick look on the steps might make sense:

  1. Access to the region is aquired.
  2. Data is bound to the template. This happens fine grained, just like filling in a from.
  3. The completed form is bound to the output.

The last step seems kind of dispenseable. I fill data to it, so it's clear I want to use it. But you have to be aware, render()is a short cut for render(template, "named-region"). So it's a discription how it is used. Thanks to this mechanism you can easily combine the building blocks of one file or even several files to an output of your choise. This leads to an surprisingly convenient re-use of those blocks.

And it gains me focus: When I'm fighting to get html, css and javaScript right I don't have to deal with 'how is the exact path to access the data?' or 'what are the exact case this button is displayed?'. It's just about 'there is logic, so it gets a name'. Very simple, very clean.

Of course there are some other engines this support for separation of template and logic like jByte (I used it for a while) or JTPL just to name a view. However, all of them seem to lack some functionality though, I decided to write Snippetory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.