You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Sebastiano Vigna <vi...@dsi.unimi.it> on 2005/04/01 18:00:42 UTC

Loading templates from classpath

I am in the following situation: I'm running a Velocity servlet in
Jetty, and everything works fine until I load the template (which
Velocity can't found). I would like getTemplate() to load the template
from the classpath (i.e., from the jar file), so to have a completely
self-contained application. I think this is possible using a classpath
loader, but after a lot of reading a couldn't find a single example (and
exploring all possibilities is daunting).

I would be immensely grateful to anybody pointing me at a code snippet
that shows how to do it...
-- 
Ciao,

                                        seba

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: creating macro libraries

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi uday,

> Till now i created a macro for a combo box , textfield, and a screen which
> gets data from a screen class  where it can be dynamically displayed .
> I need to design a screen , where that screen is divided into two parts ,
> if u select data on one part of the screen  so it will be added to the other
> part of the screen.

Do you know how it is done in regular HTML?  (Well, DHTML maybe.) 
Then you're half way there.  Just let your macro render that HTML.  :)

When I develop a dynamic page, I usually make a static one first. 
Once I get a working one, just change the variables to dynamic content
bit by bit.  And whola!  It evolves into a dynamic page, each step
working.

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: creating macro libraries

Posted by Uday Kumar <ud...@secf.com>.
Hi Shinobu,

Till now i created a macro for a combo box , textfield, and a screen which
gets data from a screen class  where it can be dynamically displayed .
I need to design a screen , where that screen is divided into two parts ,
if u select data on one part of the screen  so it will be added to the other
part of the screen.

Regards,
uday


-----Original Message-----
From: Shinobu Kawai [mailto:shinobu.kawai@gmail.com]
Sent: Saturday, April 02, 2005 1:55 PM
To: Velocity Users List
Subject: creating macro libraries


Hi uday,

> Can u pls help me regarding velocity macros .
> how to create a macro library and also i would like to use a macro  to
> embedd a tree structure in web page

Take a peek here:
     http://jakarta.apache.org/velocity/user-guide.html#Velocimacros

Some questions to help us help:
- What do you want to do?
- What have you tried so far?
- What results did you get?
- What results did you expect?

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


creating macro libraries

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi uday,

> Can u pls help me regarding velocity macros .
> how to create a macro library and also i would like to use a macro  to
> embedd a tree structure in web page

Take a peek here:
     http://jakarta.apache.org/velocity/user-guide.html#Velocimacros

Some questions to help us help:
- What do you want to do?
- What have you tried so far?
- What results did you get?
- What results did you expect?

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Sebastiano Vigna <vi...@dsi.unimi.it>.
On Wed, 2005-04-06 at 00:36 +0200, Sebastiano Vigna wrote:

> So the problem is open: how can I load a template from the jar from
> which the class is coming programmatically?

FINALLY, I got it working. It was somehow simpler than expected; my
fault 8^).

The properties you want to change must be passed to init(), or they are
useless. E.g., 

		Properties properties = new Properties();
		properties.setProperty( "resource.loader", "class" );
		properties.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() );
		Velocity.init( properties );

Once that is done, getTemplate( "the/full/path/in/the/jar" ) will work
(I thought the resource loader would have resolved relatively to the
loading class package).e:
-- 
Ciao,

                                        seba

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Sebastiano Vigna <vi...@dsi.unimi.it>.
On Fri, 2005-04-01 at 14:06 -0800, Shinobu Kawai wrote:
> Hi seba,
> 
> > You read my mind. 8^) Here's the catch: put this in the servlet.
> > 
> > protected Properties loadConfiguration( final ServletConfig config ) throws FileNotFoundException, IOException {
> >        Properties p = super.loadConfiguration( config );
> >        p.setProperty( "resource.loader", "class" );
> >        p.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() );
> >        return p;
> > }
> 
> Thanks for sharing your solution with us!  Maybe someday it'll get into the FAQ.
>      http://wiki.apache.org/jakarta-velocity/VelocityFAQ
> ## Anyone can update the wiki, BTW.  B)

BTW, it doesn't work.

I had a symlink to my template floating in the development root. That's
why it was working. 8^( I discovered it when I tried to load *another*
template.

So the problem is open: how can I load a template from the jar from
which the class is coming programmatically?
-- 
Ciao,

                                        seba


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: Loading templates from classpath

Posted by Uday Kumar <ud...@secf.com>.
Hi,

Can u pls help me regarding velocity macros .
how to create a macro library and also i would like to use a macro  to
embedd a tree structure in web page

Regards,
uday




-----Original Message-----
From: Shinobu Kawai [mailto:shinobu.kawai@gmail.com]
Sent: Saturday, April 02, 2005 3:37 AM
To: Velocity Users List
Subject: Re: Loading templates from classpath


Hi seba,

> You read my mind. 8^) Here's the catch: put this in the servlet.
>
> protected Properties loadConfiguration( final ServletConfig config )
throws FileNotFoundException, IOException {
>        Properties p = super.loadConfiguration( config );
>        p.setProperty( "resource.loader", "class" );
>        p.setProperty( "class.resource.loader.class",
ClasspathResourceLoader.class.getName() );
>        return p;
> }

Thanks for sharing your solution with us!  Maybe someday it'll get into the
FAQ.
     http://wiki.apache.org/jakarta-velocity/VelocityFAQ
## Anyone can update the wiki, BTW.  B)

> What was completely confusing me is that Velocity defines a number of
> final static constant strings for properties, but apparently not for
> *all* properties. "class.resource.loader.class" is not in, or I couldn't
> find it. Anyway...

FYI, there's one for "resource.loader".  :)

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi seba,

> You read my mind. 8^) Here's the catch: put this in the servlet.
> 
> protected Properties loadConfiguration( final ServletConfig config ) throws FileNotFoundException, IOException {
>        Properties p = super.loadConfiguration( config );
>        p.setProperty( "resource.loader", "class" );
>        p.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() );
>        return p;
> }

Thanks for sharing your solution with us!  Maybe someday it'll get into the FAQ.
     http://wiki.apache.org/jakarta-velocity/VelocityFAQ
## Anyone can update the wiki, BTW.  B)

> What was completely confusing me is that Velocity defines a number of
> final static constant strings for properties, but apparently not for
> *all* properties. "class.resource.loader.class" is not in, or I couldn't
> find it. Anyway...

FYI, there's one for "resource.loader".  :)

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Sebastiano Vigna <vi...@dsi.unimi.it>.
On Fri, 2005-04-01 at 13:43 -0800, Shinobu Kawai wrote:

> ## Let me guess; you don't want to make a velocity.properties file either.  :p

You read my mind. 8^) Here's the catch: put this in the servlet.

protected Properties loadConfiguration( final ServletConfig config ) throws FileNotFoundException, IOException {
	Properties p = super.loadConfiguration( config );
	p.setProperty( "resource.loader", "class" );
	p.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() );
	return p;
}

What was completely confusing me is that Velocity defines a number of
final static constant strings for properties, but apparently not for
*all* properties. "class.resource.loader.class" is not in, or I couldn't
find it. Anyway...

-- 
Ciao,

                                        seba


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi seba,

> ServletHandler servlets = new ServletHandler();
> context.addHandler( servlets );
> servlets.addServlet( "Query", "/*", "it.unimi.dsi.mg4j.query.QueryServlet" );
> 
> ...and it works! 8^) I think the idea is that jetty can be entirely work
> programmatically, making it ideal for embedded applications.

A quick look at the javadocs tells me you can do this:
ServletHolder holder = servlets.addServlet(...);
holder.setInitParameter("org.apache.velocity.properties", "velocity.properties")

## Let me guess; you don't want to make a velocity.properties file either.  :p

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Sebastiano Vigna <vi...@dsi.unimi.it>.
On Fri, 2005-04-01 at 12:40 -0800, Shinobu Kawai wrote:
> Hi seba,
> 
> Let's keep it on the list.  No secrets.  ;)

Ok 8^).

> If you aren't going to customise your web.xml, how are you going to
> let the servlet container know which servlet to use?  Or am I missing
> something about jetty?

Well, it couldn't be easier!

ServletHandler servlets = new ServletHandler();
context.addHandler( servlets );
servlets.addServlet( "Query", "/*", "it.unimi.dsi.mg4j.query.QueryServlet" );

...and it works! 8^) I think the idea is that jetty can be entirely work
programmatically, making it ideal for embedded applications.

> You got the picture!  A partial sample can be found here:
>      http://jakarta.apache.org/velocity/developer-guide.html#Miscellaneous%20Details

I'll have a look...
-- 
Ciao,

                                        seba


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi seba,

Let's keep it on the list.  No secrets.  ;)

> My problem is that I would like to set the loader programmatically. I do
> not want to set up web.xml or similar. I just want to start jetty and
> have the servlet initialisation code (or whatever initialisation code
> must be) set the loader to the ClasspathResourceLoader, and finally have
> templates read from the jar.

If you aren't going to customise your web.xml, how are you going to
let the servlet container know which servlet to use?  Or am I missing
something about jetty?
Anyways, _if_ you're willing to edit your web.xml, it's as simple as
setting your velocity.properties.
     http://jakarta.apache.org/velocity/tools/view/

> I have the idea that this should be doable by overriding
> loadConfiguration() and setting something either in the Velocity runtime
> or in the properties obtained by super.loadConfiguration( config ).
> Again I could not find any examples about this.

You got the picture!  A partial sample can be found here:
     http://jakarta.apache.org/velocity/developer-guide.html#Miscellaneous%20Details

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Loading templates from classpath

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi seba,

> I am in the following situation: I'm running a Velocity servlet in
> Jetty, and everything works fine until I load the template (which
> Velocity can't found). I would like getTemplate() to load the template
> from the classpath (i.e., from the jar file), so to have a completely
> self-contained application. I think this is possible using a classpath
> loader, but after a lot of reading a couldn't find a single example (and
> exploring all possibilities is daunting).
> 
> I would be immensely grateful to anybody pointing me at a code snippet
> that shows how to do it...

What have you tried so far?  Let's start from there.

Some documents you might want to read:
    http://jakarta.apache.org/velocity/developer-guide.html#Resource%20Loaders
    http://jakarta.apache.org/velocity/api/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html
    http://issues.apache.org/bugzilla/show_bug.cgi?id=22419
    http://issues.apache.org/bugzilla/show_bug.cgi?id=31703

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org