Re: Usage of ToolManager

Mike Kienenberger <[email protected]> Fri, 4 Feb 2022 09:53:20 -0500
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <CAM1yOjaq23dPNxDGrRa3CXBpGWF2wK3ui5w=iO=tbaAWPqs_LA@mail.gmail.com>
--0000000000005c279605d7326ac5
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Ok.  looking back at your original code again (I didn't see the diff
notation as important in my quick glance)...

$context isn't likely to exist.   "$x" translates to "context.get(x)"

There's no line to configure (load the tools into) your ToolManager.
From the link you provided it's done with:

manager.configure("/path/to/my/configuration.xml");


Looking further here shows that you need a more extensive constructor to
pick up a default set of tools without providing configuration:

https://velocity.apache.org/tools/3.1/generic.html

/* Create the manager. autoConfigure and includeDefaults are both
booleans which default to true. */

ToolManager manager =3D new ToolManager(autoConfigure, includeDefaults);



On Fri, Feb 4, 2022 at 2:08 AM Stefan Gro=C3=9Fhauser <
[email protected]> wrote:

>
> Hello Mike,
>
>
> my code listing was rather a patch, note the - und + symbols at the
> beginning of each line.
> It was meant to indicate that the - line is replaced by the + lines.
>
>
> Thank you very much for showing that the tools can be put() manually like
> that, so that I can get away without using ToolManager.
> That answers my question.
>
>
>
> Thank you!
>
>
> cheers
> Stefan
>
>
>
>  Von:   Mike Kienenberger <[email protected]>
>  An:   Velocity Users List <[email protected]>
>  Kopie:   Stefan Gro=C3=9Fhauser <[email protected]>
>  Gesendet:   03.02.2022 15:20
>  Betreff:   Re: Usage of ToolManager
>
> I haven't used Velocity 2 or ToolManager 3, but the basics of Velocity ar=
e
> very simple.
>
> You create a VelocityContext (which is effectively a Java.util.Map).
> You "put" objects into it with String keys.
> You reference these string keys in your template.
>
> In the setup code:
>
> VelocityContext context =3D ...
> context.put("anything", new Object());
>
> in the template:
>
> $anything
>
>
> VelocityContexts differ from Maps in that they can also wrap one or more
> other VelocityContexts to which they will delegate lookups.
>
> Generally it works along the lines of
>
> VelocityContext myAppContext is created with a delegate of ToolContext.
> So it first tries to find your key in myAppContext.   If not found, it
> will
> try to find your key in ToolContext.
>
> So a ToolManager context for Velocity 1.x  is just another
> VelocityContext.
>
> Glancing at  your code, I see that you first created a myAppContext.
> Then you create a ToolManager context (with the same name?)
>
> But I don't see that you've linked these together.
>
> Normally, you'd do something like this to create your myAppContext:
>
>         ToolboxContext toolCtx =3D mgr.getToolboxContext(null);
>         VelocityContext myAppContext =3D new VelocityContext(toolCtx);
>
> Again, though, the only reason to use a ToolManager is to make it easier
> to
> configure a context.
> Either because the ToolManager supports grabbing the objects from file
> (ie,
> XMLToolboxManager.class)
> or because it grabs them from a hardcoded list or reflectively-generated
> list.   And then it "put"s these object into a VelocityContext that it
> makes available to you via getToolboxContext();
>
> You can certainly start by just dumping objects directly into your
> context.
>
> context.put("esc", new EscapeTool());
> context.put("request", httpRequest);
> context.put("context", context);
>
> Or creating your own ToolManager:
>
> class MyToolManager implements ToolManager {
>     ToolboxContext getToolboxContext(Object initData) {
>         VelocityContext context =3D new VelocityContext();
>         context.put("esc", new EscapeTool());
>         context.put("request", httpRequest);
>         context.put("context", context);
>         return context;
>      }
>
>     void addTool(ToolInfo info) {
>          // you can get more fancy by implementing this to fetch tool
> config rather than hard coding classes above
>     };
> }
>
> Because Velocity is such a simple idea, you can review the Velocity sourc=
e
> code if you want to see what's going on internally with Velocity contexts
> and ToolManagers..
>
>
>
>
>
>
>
> JETZT NEWSLETTER ABONNIEREN:
> https://hammerbacher.com/newsletter-anmeldung/
> Hammerbacher GmbH Gesch=C3=A4ftsf=C3=BChrer Bernhard Hammerbacher, Ursula
> Hammerbacher Registergericht N=C3=BCrnberg HRB 10908
>         Hausanschrift
> Daimlerstra=C3=9Fe 4-6
> D 92318 Neumarkt        Telefon
> +49(0)9181
> 2592-0  Telefax
> +49(0)9181
> 2592-28         E-Mail
> [email protected]
> www.hammerbacher.com
>
>
>
> Haftungsausschluss / Disclaimer
> Die Informationen, die in dieser Kommunikation enthalten sind, sind
> ausschlie=C3=9Flich und allein f=C3=BCr den Empf=C3=A4nger bestimmt. Die =
Verwendung durch
> Dritte ist untersagt. Die Firma Hammerbacher GmbH ist nur f=C3=BCr die vo=
n ihr
> eingegeben Informationen verantwortlich, jedoch nicht f=C3=BCr die einwan=
dfreie
> =C3=9Cbertragung oder im Zusammenhang mit der =C3=9Cbertragung oder dem E=
mpfang
> eingetretene Ver=C3=A4nderungen oder Verz=C3=B6gerungen.
> Diese E-Mail enth=C3=A4lt vetrtrauliche und/oder rechtlich gesch=C3=BCtzt=
e
> Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mai=
l
> irrt=C3=BCmlich erhalten  haben, informieren Sie bitte sofort den Absende=
r und
> vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
> Weitergabe dieser Mail ist nicht gestattet.
>

--0000000000005c279605d7326ac5--