You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by sean liu <se...@gmail.com> on 2005/11/04 05:42:20 UTC
[jexl] how to implement variable replacement in expressions
Hi guys:
I want to use jexl to implement variable replacement in expressions. For
example:
expression: "Hello: ${person.name <http://person.name/>}, your age is ${
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!
Re: [jexl] how to implement variable replacement in expressions
Posted by Dion Gillard <di...@gmail.com>.
Nope, the JEXL syntax hasn't changed.
I'm guessing they:
a) Based their code on something else, possbly Jelly, or
b) Never tested it....
On 11/7/05, sean liu <se...@gmail.com> wrote:
> 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 <di...@gmail.com> wrote:
> >
> > On 11/3/05, sean liu <se...@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
> >
> >
>
>
--
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
Re: [jexl] how to implement variable replacement in expressions
Posted by sean liu <se...@gmail.com>.
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 <di...@gmail.com> wrote:
>
> On 11/3/05, sean liu <se...@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
>
>
Re: [jexl] how to implement variable replacement in expressions
Posted by Dion Gillard <di...@gmail.com>.
On 11/3/05, sean liu <se...@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/>}, your age is ${
Try with the following Expression in double quotes:
'Hello ' + 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