You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Thomas Fahrmeyer <th...@einsurance.de> on 2001/02/06 16:41:41 UTC

automatic context filling ??

Hi,

is there a way to do the following:
	UtilityClass.fillContext(context, "user.name", "scott");

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("user.name") // because all the form
elements are named like the
									 // Velocity context objects
	UtilityClass.fillContext(context, "user.name", 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.
-----------------------------------------



Re: automatic context filling ??

Posted by Jon Stevens <jo...@latchkey.com>.
on 2/6/01 7:41 AM, "Thomas Fahrmeyer" <th...@einsurance.de>
wrote:

> is there a way to do the following:
> UtilityClass.fillContext(context, "user.name", "scott");
> 
> 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("user.name") // because all the form
> elements are named like the
> // Velocity context objects
> UtilityClass.fillContext(context, "user.name", 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.

Thomas,

You are close to developing a business object, but not quite there yet. :-)
The right way to do this is to have a object class: User

public class User
{
    setName(String name)
    String getName()
}

Then, what you would do is this:

User user = new User();
user.setName("bob");
context.put ("user", user);

Now, if you are using Turbine's ParameterParser class, you could do
something like this:

User user = new User();
data.getParameters().setProperties(user);
context.put("user", user);

That would use introspection to look for form fields names which match the
same method names.

The ParameterParser class is here:

<http://www.working-dogs.com/turbine/cvsweb/index.cgi/turbine/src/java/org/a
pache/turbine/util/ParameterParser.java?rev=1.25&content-type=text/x-cvsweb-
markup>

Look at the setProperties() method for details. Let me also state that this
is why you should use Turbine...the code that you need to take advantage of
is already there. :-)

thanks,

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/> | <http://java.apache.org/turbine/>