5

I'm pretty new to JSP. So far it seems that the flow of processing is very much Java runs first, then populates a JSP template.

I am wondering if there is a way from within Java to utilize a JSP template. What I mean is, imagine I had a simple "SimpleDiv.jsp" template on classpath like this:

<div id="${id}" class="${class}">
    ${content}
</div>

And then from within an arbitrary Java file (perhaps not even running on a servlet), I could do something like this:

private String getDivHtml( id, html ) {
    Template simpleDiv = TemplateLoader.load("SimpleDiv.jsp");
    simpleDiv.set("id", id);
    simpleDiv.set("class", Whatever.CLASS_NAME);
    simpleDiv.set("content", html);

    return simpleDiv.toString();
}

This is a pretty simplistic example so don't get caught up on the details of that. Main question is -- can I pull in a JSP template in Java and cause it to generate HTML inline?

jwl
  • 10,268
  • 14
  • 53
  • 91

4 Answers4

4

Freemarker and Velocity are very popular for generating content from templates, you might try one of them. Since JSPs are implemented as servlets (and the JSP spec defines them as webcomponents) they are tied to the servlet container.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • How similar are the templating languages and capabilities to JSP? Would be good if the syntax were as similar as possible (even if they lack features like taglibs, etc) – jwl Sep 18 '13 at 16:43
  • @Joe: it depends what you want, referencing beans is very close, control structures have different syntax. see http://freemarker.org/docs/dgui_quickstart_template.html – Nathan Hughes Sep 18 '13 at 16:49
  • Velocity and FreeMarker are quite different (from JSP) - they don't use XML tags. But in my experience this is a good thing; the tags are easier to spot and edit. Most worthwhile IDEs/editors will syntax highlight them too. There is no presumption in either of these libraries that the generated output will a) go to a web client (i.e. be sent back from a servlet container), or b) actually output HTML or XML. Neither supports taglibs (from memory) but they *do* support other means (better ones, IMHO) for creating re-usable content 'tags'. – Paul Sep 18 '13 at 16:53
  • @Paul The reason I'd want similarity is that we will still be using real JSP templates also. So either the additional templating language should be very similar (so people can pretend its the same) or very different (so people don't mistakenly thing it's the same). Otherwise it's uncanny valley issues! – jwl Sep 18 '13 at 17:11
  • @zhong.j.yu - ditto Nathan's comment, this is the right answer! – jwl Sep 18 '13 at 17:12
  • @Joe: i don't think velocity or freemarker will be subject to the uncanny-valley problem. they are very different from jsp. – Nathan Hughes Sep 18 '13 at 17:15
2

There's no simple way to accomplish this using plain JSP. There are related Q/As in the site explaining how to do it:

Another option using plain JSP would be using external frameworks to accomplish the task like Apache Tiles and SiteMesh (mentioned here: JSP template implementation (Composite View Pattern)).

If you can, upgrade to Facelets, the current view technology since Java EE 6. This technology already provides built-in template system as explained here and here.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
2

Consider this subset of JSP which has no dependency on servlet:

http://jstp.sourceforge.net/manual.html

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
0

Jsp is a inner servelt, a java class, normally, it outputs HTML when it finishes executing.

Maybe you should just treat the jsp file as a pure string, the "simpeDiv.set" method just do replacing works: replace ${key} to ${value}

sinfere
  • 927
  • 8
  • 9