serving a static html page

937 views
Skip to first unread message

Tommy Chheng

unread,
May 29, 2012, 2:50:09 PM5/29/12
to scalat...@googlegroups.com
I have a static html page in webapp/WEB-INF/views/dashboard.html

How can I render this in a get handler?

I tried both these but get null pointer exceptions. How should i be referencing the path correctly?

  get("/dashboard"){
    contentType = "text/html"
    val path = "/views/dashboard.html"
    new java.io.File( getServletContext().getResource(path).getFile )
  }

  get("/dashboard"){
    contentType = "text/html"
    val path = "/views/dashboard.html"
    val input = getServletContext().getResourceAsStream(path)
    org.apache.commons.io.IOUtils.readLines(input).mkString("")
  }


Leif Warner

unread,
May 29, 2012, 5:43:57 PM5/29/12
to scalatra-user
If it's returning nulls, sounds to me like it's not finding that
file. I forget what the root of of ServletContext.getResource() is -
I'm using to having things in WEB-INF/classes to read them off the
classpath.
I believe you might need to put the "/WEB-INF" as part of your path,
like "/WEB-INF/views/dashboard.html".
However, if you're just serving a static file, why have it in WEB-
INF? That directory's more for code things. ScalatraServlet has a
handler that will serve a static file in the top-level of your webapp
if no route is defined for it:
https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/ScalatraServlet.scala#L65
That's the default behaviour for Filters, as well.

If you really do need to muck with the request / response
programaticlly, yes, you can return a File reference, and Scalatra
will serve it, with the code here:
https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/ScalatraServlet.scala#L65

The ServletContext is available for you as a variable called
"servletContext":
http://www.scalatra.org/2.1/book/#ServletContext
So returning new File(servletContext.getResource("/WEB-INF/views/
dashboard.html")) should work.
You can also add to that renderPipeline, perhaps something that will
turn URLs into Files for you, and do the same thing.
-Leif

Ivan Porto Carerro

unread,
May 29, 2012, 5:49:46 PM5/29/12
to scalat...@googlegroups.com
You can move the file out of the WEB-INF directory

so instead of webapp/WEB-INF/views/dashboard.html it should be
webapp/views/dashboard.html


and things should be served automatically

Tommy Chheng

unread,
May 29, 2012, 6:51:22 PM5/29/12
to scalat...@googlegroups.com


On Tuesday, May 29, 2012 2:43:57 PM UTC-7, Leif Warner wrote:
If it's returning nulls, sounds to me like it's not finding that
file.  I forget what the root of of ServletContext.getResource() is -
I'm using to having things in WEB-INF/classes to read them off the
classpath.
I believe you might need to put the "/WEB-INF" as part of your path,
like "/WEB-INF/views/dashboard.html".
However, if you're just serving a static file, why have it in WEB-
INF?  That directory's more for code things.  ScalatraServlet has a
handler that will serve a static file in the top-level of your webapp
if no route is defined for it:
https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/ScalatraServlet.scala#L65
That's the default behaviour for Filters, as well.

If you really do need to muck with the request / response
programaticlly, yes, you can return a File reference, and Scalatra
will serve it, with the code here:
https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/ScalatraServlet.scala#L65

The ServletContext is available for you as a variable called
"servletContext":
http://www.scalatra.org/2.1/book/#ServletContext
So returning new File(servletContext.getResource("/WEB-INF/views/
dashboard.html")) should work.

Thanks for the informative post! I ended up using the past tip. 
 

Reply all
Reply to author
Forward
0 new messages