commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sean liu <seanli...@gmail.com>
Subject Re: [jexl] how to implement variable replacement in expressions
Date Mon, 07 Nov 2005 05:21:38 GMT
Thanks a lot for your helps, Dion.
I have some confusion about jexl.
In OReilly book "Jakarta Commons Cookbook", there is a chapter called
"Templating" in which there is the following code snippets:

  import org.apache.commons.jexl.Expression;

import org.apache.commons.jexl.ExpressionFactory;

import org.apache.commons.jexl.JexlContext;

import org.apache.commons.jexl.JexlHelper;



Opera opera = new Opera( );
opera.setName("The Magic Flute");
opera.setComposer("Mozart");
opera.setYear(1791);



String expr =

    "${opera.name <http://opera.name>} was composed by ${opera.composer} in " +

    "${opera.year}.";


*Expression e = ExpressionFactory.createExpression( expr );*
*JexlContext jc = JexlHelper.createContext( );*
*jc.getVars( ).put("opera", opera);*
*String message = (String) e.evaluate(jc);*


System.out.println( message );

I don't know why these code ssnippets doesn't work.Have the jexl
expression syntax changed?

On 11/4/05, Dion Gillard <dion.gillard@gmail.com> wrote:
>
> On 11/3/05, sean liu <seanliugm@gmail.com> wrote:
> > Hi guys:
> > I want to use jexl to implement variable replacement in expressions. For
> > example:
> > expression: "Hello: ${person.name <http://person.name> <
> http://person.name/>}, your age is ${
>
> Try with the following Expression in double quotes:
> 'Hello ' + person.name <http://person.name> + ' <http:// ' + person.name<http://person.name>+
'/>, your age is
> ' + person.age
>
>
> > person.age}!"
> > public class Person {
> > private String name;
> > private int age;
> > //setters,getters
> > }
> > i use the following codes:
> > Person person = new Person();
> > person.setName(sean);
> > person.setAge(26);
> >
> > Expression e = ExpressionFactory.createExpression(expression);
> >
> > JexlContext jc = JexlHelper.createContext();
> >
> > jc.getVars().put("person", person);
> >
> > message = (String) e.evaluate(jc);
> >
> > System.out.println(message);
> >
> > It doesn't work.I dont know what's wrong with expression. I have never
> used
> > jexl before, so if anyone can give me some help, thanks a lot!
> >
> >
>
>
> --
> http://www.multitask.com.au/people/dion/
> "You are going to let the fear of poverty govern your life and your
> reward will be that you will eat, but you will not live." - George
> Bernard Shaw
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message