velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <>
Subject Re: automatic context filling ??
Date Wed, 07 Feb 2001 17:55:32 GMT
Thomas Fahrmeyer wrote:
> Hi,
> what I want to have is may be application specific, but I asked that because
> I think that
> functionality is already implemented in Velocity. If a variable $
> is in the template, Velocity have to find the object in the context, and
> call the getName Method.
> The mechanism to find objects,subobjects and properties is there. I just
> want to use this mechanism to address a object in the context with
> "". Then call setName(given value) instead of getName like the
> template engine it does.

Because Jon's reponse to your original post was much more useful than
mine wrt actually solving your problem, commenting on just on the
technical aspects, and assuming I have it right :

If you put something into the context with the key ""

context.put("", foo );

then things are going to get confusing.  Because, when you do something
like :


Velocity will look for "user" in the context, not "".  Once it
finds something for "user", it will try to find a getter via getname(),
getName(), or get("name")

So trying to outsmart it by putting in something as "" will
lead to pathos and despair. :D

> I don't want to integrate such funtionality into Velocity's utility classes,
> but I want implement it by myself. I want to know which classes are involved
> in recognizing the objects in the context and calling the appropriate
> methods.

I don't think you want to do this.  Really.  You are going to make a
mess, make your life miserable, and since I live vicariously through
Velocity users, mine too by extension.

The parts that do the introspection are deep inside, and tied to the
internals of the template engine.  It uses standard Java stuff, like
java.lang.reflect classes.  But I really think that Turbine has a set of
tools that you can model your stuff after, or just steal outright, and a
great community to draw experience and help from.

But to answer the question, look in o.a.v.runtime.parser.node.ASTMethod,
ASTIdentifier, o.a.v.util.introspection.*.  Then go to Turbine :)

> The form refilling I wanna implement should do the following:
> 1. all my form input fields have names like "" (corresponding to
> reading values out of the context)
> 2. to put the values from the form fields into the context once again (for a
> re-displaying if the form data are not valid) I want read the value with
>         value = request.getParameter("")
> and  store it in the context with:
>         fillContext(context, "", value);

Well, a trick that I do when I am lazy (most of the time) is to use a
Hashtable as my object, so the parameters can be arbitrary (I don't have
to write a class with get/set per property).  So if on a desert island,
I would do something like :

public void fillContext( Context c, String prop, String val )
	// split the prop string ob.key to get the ob and key

	// then

	Hashtable h = (Hashtable) c.get("ob");

	h.put("key", val );


> Context contains all the object I want to modify.
> This process can just implemented in a loop and I have not worry about
> reading and setting the values in context. If the template is displayed once
> again I'll get all the prefilled values for the form fields. (form field
> values are defined with: value="$")
> Is that a bad approach ?

I guess not.  but this is something that has been done over and over, or
at least solving the problem (not necessarily this solution), and maybe
there is a good solution in Turbine.

> To Steve: ParameterParser could be a solution, but what about aggregated
> objects like:
>         user.address
> and I want to acces the "street" from the address: user.address.street ??
> Another short question to Turbine: I missed a good way to control the screen
> flow (or just not found ;), like Struts it provides. Is there something
> similar ?
> Thomas
> -----Original Message-----
> From: Geir Magnusson Jr. []
> Sent: Tuesday, February 06, 2001 6:28 PM
> To:
> Subject: RE: automatic context filling ??
> Huh?
> More inline...
> > -----Original Message-----
> > From: Thomas Fahrmeyer []
> > Sent: Tuesday, February 06, 2001 10:42 AM
> > To: Velocity-User
> > Subject: automatic context filling ??
> >
> >
> > Hi,
> >
> > is there a way to do the following:
> >       UtilityClass.fillContext(context, "", "scott");
> In Java?  Why not just set your objects *before* putting them into the
> context?
> If I am reading this right, it sounds like an application design issue more
> than a deficiency in the Velocity toolset.
> Can you explain this again if I didn't get it?
> geir
> > It should do: Look in the given context for a object named
> > "user", then call
> > setName("scott")
> > That would be very cool. I wanna use it to pre-fill a form. I
> > want to create
> > a context and put all the
> > inital objects like "user" into that context. Then validate
> > the form data.
> > If the form is invalid I would like to do:
> >       String value = request.getParameter("") //
> > because all the form
> > elements are named like the
> >
> >        // Velocity context objects
> >       UtilityClass.fillContext(context, "", value);
> >
> > Then I use this context to display the data in the template.
> >
> > I know you are confused once again ;), but I think you can
> > imagine what I
> > wanna have.
> >
> > How can I achieve that, which classes are involved. Can I
> > customize Velocity
> > behavior.
> >
> > bye
> > Thomas
> >
> >       (O) (O)
> > ----^^^  °  ^^^--------------------------
> > Das was wir haben, können wir verlieren
> > durch den masslosen Wunsch nach mehr.
> > -----------------------------------------
> >
> >

Geir Magnusson Jr.                     
Velocity : it's not just a good idea. It should be the law.

View raw message