How do I get scalatra to serve static files ?

570 views
Skip to first unread message

letronje

unread,
Nov 28, 2010, 6:47:07 AM11/28/10
to scalatra-user
How do I get scalatra to serve static files ?

Ross A. Baker

unread,
Nov 28, 2010, 2:17:56 PM11/28/10
to scalat...@googlegroups.com
On Sun, Nov 28, 2010 at 6:47 AM, letronje <manojp...@gmail.com> wrote:
> How do I get scalatra to serve static files ?

That depends on whether the files are inside or outside your WAR.

If the file is inside the WAR, then the easiest way is to use
ScalatraFilter. Any requests that don't match a route will pass
through to the default servlet, which will serve the file. The root
of your WAR file maps to the context path of your application. If
you're using a ScalatraServlet, you can call the default servlet
explicitly by calling
servletContext.getNamedDispatcher("default").forward(request,
response).

If the file is outside your WAR, then I would return a java.io.File
object from your actions for the file to be served. You'll also need
to override renderResponseBody to copy your File object to the
response's output stream. (This should be the default framework
behavior -- I just created a ticket:
https://github.com/scalatra/scalatra/issues/issue/19).

--
Ross A. Baker
ba...@alumni.indiana.edu
Indianapolis, IN, USA

Ross A. Baker

unread,
Nov 29, 2010, 9:39:19 PM11/29/10
to scalat...@googlegroups.com
On Sun, Nov 28, 2010 at 2:17 PM, Ross A. Baker <ba...@alumni.indiana.edu> wrote:
> If the file is outside your WAR, then I would return a java.io.File
> object from your actions for the file to be served.  You'll also need
> to override renderResponseBody to copy your File object to the
> response's output stream.  (This should be the default framework
> behavior -- I just created a ticket:
> https://github.com/scalatra/scalatra/issues/issue/19).

This is now the default behavior in the latest snapshot -- no need to
touch renderResponseBody.

Reply all
Reply to author
Forward
0 new messages