JSTL/JSP EL (Expression Language) in a non JSP (standalone) context
Asked Answered
D

10

13

Can anyone recommend a framework for templating/formatting messages in a standalone application along the lines of the JSP EL (Expression Language)?

I would expect to be able to instantiate a an object of some sort, give it a template along the lines of

Dear ${customer.firstName}. You order will be dispatched on ${order.estimatedDispatchDate}

provide it with a context which would include a value dictionary of parameter objects (in this case an object of type Customer with a name 'customer', say, and an object of type Order with a name 'order').

I know there are many template frameworks out there - many of which work outside the web application context, but I do not see this as a big heavyweight templating framework. Just a better version of the basic Message Format functionality Java already provides

For example, I can accomplish the above with java.text.MessageFormat by using a template (or a 'pattern' as they call it) such as

Dear {0}. You order will be dispatched on {1,date,EEE dd MMM yyyy}

and I can pass it an Object array, in my calling Java program

new Object[] { customer.getFirstName(), order.getEstimatedDispatchDate() };

However, in this usage, the code and the pattern are intimately linked. While I could put the pattern in a resource properties file, the code and the pattern need to know intimate details about each other. With an EL-like system, the contract between the code and the pattern would be at a much higher level (e.g. customer and order, rather then customer.firstName and order.estimatedDispatchDate), making it easier to change the structure, order and contents of the message without changing any code.

Detective answered 18/9, 2008 at 11:15 Comment(0)
I
12

You can just use the Universal Expression Language itself. You need an implementation (but there are a few to choose from). After that, you need to implement three classes: ELResolver, FunctionMapper and VariableMapper.

This blog post describes how to do it: Java: using EL outside J2EE.

Influent answered 18/9, 2008 at 11:46 Comment(1)
Thanks. That looks really useful. Unfortunately, a quick attempt replacing my MessageFormat based code failed because I am using Java 1.4 (both Jasper and Juel are compiled for 1.5). I might try to find an older version, or building my own JAR from sourceDetective
W
4

StringTemplate is a more lightweight alternative to Velocity and Freemarker.

Wagonage answered 23/9, 2008 at 16:38 Comment(0)
L
3

I would recommend looking into Apache Velocity. It is quite simple and lightweight.

We are currently using it for our e-mail templates, and it works very well.

Lucullus answered 18/9, 2008 at 11:28 Comment(1)
I find Velocity to be too heavy for this task - remember I am comparing with the Java MessageFormatDetective
B
2

The idea of using EL itself outside of Java EE was advocated by Ed Burns and discussed on The Server Side. Tomcats implementation ships in a separate JAR but I don't know if it can be used outside the server.

Bellyful answered 18/9, 2008 at 11:44 Comment(0)
A
2

You can use Casper very similar to jsp and easy to use : Casper

Adaminah answered 12/12, 2010 at 19:4 Comment(0)
K
2

I would go for the Spring Expression language:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html

A few examples which demonstrate the power (the first two are from the documentation):

int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);

String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);

// weekday is a String, e.g. "Mon", time is an int, e.g. 1400 or 900
{"Thu", "Fri"}.contains(weekday) and time matches '\d{4}'

Expressions can also use object properties:

public class Data {
    private String name; // getter and setter omitted
}

Data data = new Data();
data.setName("John Doe");

ExpressionParser p = new SpelExpressionParser();
Expression e = p.parseExpression("name == 'John Doe'");
Boolean r = (Boolean) e.getValue(data); // will return true

e = p.parseExpression("Hello " + name + ", how are you ?");
String text = e.getValue(data, String.class); // text will be "Hello John Doe, how are you ?"
Konya answered 22/2, 2015 at 20:45 Comment(0)
R
1

You might want to look at OGNL which is the kind of library you are after. OGNL can be reasonably powerful, and is the expression language used in the WebWork web framework.

Rileyrilievo answered 18/9, 2008 at 11:29 Comment(0)
D
1

Re: Jasper and Juel being built for 1.5: And then I discovered RetroTranslator (http://retrotranslator.sourceforge.net/). Once retrotranslated, EL and Jasper works like a charm

Detective answered 25/9, 2008 at 10:49 Comment(0)
K
0

Freemarker would do exactly what you need. This is a template engine with a syntax very similar to JSP :

http://freemarker.org/

Killam answered 18/9, 2008 at 11:49 Comment(1)
I think Freemarker is a little too heavy for this task too (see previous comment about Velocity) - remember I am comparing with the Java MessageFormatDetective
D
0

AAh. Whereas with MessageFormat, I can do

Dear {0}. Your order will be dispatched on {1,date,EEE dd MMM yyyy}

where parameter #1 is a Date object and it gets formatted according to the pattern, there is no equivalent in EL.

In JSP, I would have used, perhaps, a format tag. In this standalone example, I am going to have to format the Date as a String in my code prior to evaluating the expression.

Detective answered 25/9, 2008 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.