Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 671
Vihung Avatar asked Sep 18 '08 11:09

Vihung


People also ask

What is EL expression in JSP?

The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc. There are many implicit objects, operators and reserve words in EL. It is the newly added feature in JSP technology version 2.0.

Which of the following is a valid EL expression language statement in JSP?

Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.

What is expression in JSP give JSP example for it?

A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client.

Is El ignored in JSP?

The default mode for JSP pages delivered using a descriptor from Servlet 2.3 or before is to ignore EL expressions; this provides backward compatibility.


1 Answers

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.

like image 134
Garth Gilmour Avatar answered Nov 15 '22 18:11

Garth Gilmour