Thanks Nathan, It worked and here is how I implemented, I guess for others.
1) On application startup. //Add context to application attribute. Velocity.setApplicationAttribute("javax.servlet.ServletContext", servlet.getServletContext()); //Specify path to props file, is there another way????? String propsFile = servlet.getServletContext().getRealPath("/WEB-INF/config/velocity.properties "); Velocity.init(propsFile); Velocity.properties file, commented out file.resource.loader #----------------------- resource.loader = webapp webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader webapp.resource.loader.path = /WEB-INF/vTemplates/ webapp.resource.loader.cache = false webapp.resource.loader.modificationCheckInterval = 2 #----------------------- And I started it and it worked! I also like the idea of just creating an instance of a "Tool", and just adding it to the context individually. Thanks -----Original Message----- From: Edward Song [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 24, 2006 12:07 PM To: 'Velocity Users List' Subject: RE: Web App and template .vm Thanks Nathan, I'll give your suggestion a shot. -----Original Message----- From: Nathan Bubna [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 24, 2006 12:11 PM To: Velocity Users List Subject: Re: Web App and template .vm On 1/24/06, Edward Song <[EMAIL PROTECTED]> wrote: > Hi, > I have somewhat of a related question. > > I would like to integrate VelocityViewServlet or VelocityStruts with > my current application. My web application currently runs struts 1.2 > as its web framework. > > I currently am using Velocity to generate XML files to be written to > the server and generate emails to be sent to users. cool. > My current configuration has Velocity running as a "general application". > With this style of configuration I need to specify an absolute path to > my templates rather than using a relative directory path from my Web > Root directory to locate my velocity templates (.vm). I also call > Velocity.init() during application startup, to initialize the velocity > engine. sounds fine, but you would be better served by using the WebappLoader from VelocityTools instead of the default FileResourceLoader. more below.... > I feel that there may be another way of doing things. Am I doing > something wrong in not leveraging the VelocityViewServlet, as some of > the objects written out will be stored in the HttpServletRequest, > HttpSession, ServletContext, as well as other benefits such as GenericTools, etc. > > The reason I chose not to use VelocityViewServlet or VelocityStruts, > is because I am not using Velocity to create my presentation, rather I > use JSP's, JSTL, etc. I use Velocity to solely generate the text/HTML > email string, and generate XML data files on my server. The > VelocityViewServlet is tightly coupled with the HTTPResponse object, > and I want to stay inside struts for the most part. That is why I > chose to use Velocity as a general application, which does work fine > for my case. the VelocityViewServlet is for the http view/presentation layer. it is designed to replace or work alongside JSP in that capacity. it is not designed to generate email or server-side-only XML. so, i think you made the right choice in using Velocity alone. however, you could still be benefited by using other portions of VelocityTools. the WebappLoader would be especially handy, and some of the generic tools might as well (which you can directly create and add to your context without the help of a toolbox). > However, everytime, I post from test to production, you can think of > the headaches that come with absolute path configuration and it > doesn't take advantage of tomcat deployment architecture. yeah, use http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/v iew/servlet/WebappLoader.html you can use it just like any other resource loader (http://jakarta.apache.org/velocity/docs/developer-guide.html#Configuring%20 Resource%20Loaders) but you will have to put the javax.servlet.ServletContext into Velocity's application attributes (http://jakarta.apache.org/velocity/docs/developer-guide.html#Application%20 Attributes) before you initialize the velocity engine or singleton (http://jakarta.apache.org/velocity/docs/developer-guide.html#To%20Singleton %20Or%20Not%20To%20Singleton...) > So, is there a way I can utilize VelocityViewServlet and Velocity > Struts without having it so tightly binded to the presentation layer? > All I want to do is specify relative paths for my velocity > configurations and have the ability to call Velocity.mergeTemplate(). > > Are these steps correct? nope :) > 1) Add to Web.xml > > <servlet> > <servlet-name>velocity</servlet-name> > > <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServ > let</s > ervlet-class> > <init-param> > <param-name>org.apache.velocity.toolbox</param-name> > <param-value>/WEB-INF/toolbox.xml</param-value> > </init-param> > <init-param> > > <param-name>org.apache.velocity.properties</param-name> > > <param-value>/WEB-INF/config/velocity.properties</param-value> > </init-param> > <load-on-startup>10</load-on-startup> > </servlet> > > <servlet-mapping> > <servlet-name>velocity</servlet-name> > <url-pattern>*.vm</url-pattern> > </servlet-mapping> that's fine, but it's more than you need. > 2) Make sure toolbox.xml and velocity.properties are in the proper > locations. In velocity.properties relative paths from the Web Root > directory are valid. That's just an assumption I have. not sure i understand what you mean by that second sentence. > 3) Start the application. > > What I see is kind of weird. What I had hoped to happen was that > Velocity.init(properties) would be called when my application launched. > This way hopefully I can use velocity throughout my app, and use > relative paths to my web root directory. > > It seems to me that the engine never started as I see no log file for > velocity. > > What am I doing wrong? the VelocityViewServlet no longer uses the Velocity singleton. instead, it uses a VelocityEngine. this means that Velocity.init(properties) was not called. > > Regards, > Edward Song > > -----Original Message----- > From: Nathan Bubna [mailto:[EMAIL PROTECTED] > Sent: Monday, January 23, 2006 10:46 AM > To: Velocity Users List > Subject: Re: Web App and template .vm > > Have you read: http://jakarta.apache.org/velocity/webapps.html? > > if you are using the VelocityViewServlet with its default properties, > then you just put the template files in the root directory of the > webapp and it will find them there. > > On 1/22/06, Graham Renney <[EMAIL PROTECTED]> wrote: > > Hi > > > > I am struggling to get a web app to see the .vm template file unless > > I place it in a physical location such as d:/template.vm and point > > the velocity engine to that location. What I want to do is place it > > in my war - but velocity does not find the template. Where do I > > place it in the war and how do I tell the engine where it is? > > > > Thanks > > Graham Renney > > > > > > This message and any attachments are confidential and intended > > solely for > the addressee. If you have received this message in error, please > notify Discovery immediately, telephone number +27 11 529 2888. Any > unauthorised use; alteration or dissemination of the contents of this > email is strictly prohibited. In no event will Discovery or the sender > be liable in any manner whatsoever to any person for any loss or any > direct, indirect, special or consequential damages arising from use of > this email or any linked website, including, without limitation, from > any lost profits, business interruption, loss of programmes or other > data that may be stored on any information handling system or > otherwise from any assurance that this email is virus free even if > Discovery is expressly advised of the possibility of such damages. Discovery is an Authorised Financial Services Provider. > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]