I found a solution.  I subclassed VelocitylayoutServlet and overrode
doRequest method as follows.  I am assuming you have set up your Java web
server (e.g. Tomcat) to send all content thru this servlet.  This servlet
then farms requests to *.vm to Velocity layout infrastructure, and for all
other requests, it streams binary content from classpath tot the browser.

public class MyVelocityLayoutServlet extends VelocityLayoutServlet {

/**
    * Override doRequest, to handle static content
    */
   protected void doRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
       String path = ServletUtils.getPath(request);
       String mt=getServletContext().getMimeType(path);
     //if (mt!=null) response.setContentType(mt);
       String extension = "vm";
       if (path.indexOf(".") >= 0) {
           String[] pathArr = path.split("\\.");
           extension = pathArr[pathArr.length - 1].toLowerCase();
       }

       if (extension.equals("vm")) {
           log.trace("Sending to VelocityLayoutServlet: Path=" + path + ";
MIME-type=" + mt + "; extension=" + extension);
           super.doRequest(request, response);
       } else {
           log.trace("Streaming to browser: Path=" + path + "; MIME-type="
+ mt + "; extension=" + extension);
           response.setContentType(mt);
           InputStream in = this.getClass().getResourceAsStream (path);
           OutputStream out = response.getOutputStream();
           int b;
           try {
               while ((b = in.read()) != -1) {
                   out.write(b);
               }
           } finally {
       if (in != null) {
           in.close();
       }
       if (out != null) {
           out.close();
       }
           }
       }
   }

---------- Forwarded message ----------
From: David Donohue <[EMAIL PROTECTED]>
Date: Apr 15, 2007 12:31 AM
Subject: ClasspathResourceLoader trouble loading images, css
To: Velocity Users List <[email protected]>

I am enjoying using VelocityLayoutServlet with ClasspathResourceLoader.  VM
files load great, and the layout loads wonderfully too. The trick is: how
can I get images, CSS, and other static content to load via classpath as
well?  Or how can I get them load at all?
THANKS!
David

Reply via email to