As I’m working with my first Scalatra project, I automatically think of using WebJars to manage Javascript library dependencies, since it’s more convenient and seems like a good practice. Though there’s no official support for Scalatra framework, the installation process is not very complex. But this doesn’t mean I didn’t spend much time on this. I’m still a newbie to Scala, and there’s only a few materials on this subject.
Add WebJars Dependency in SBT Build File
Scalatra uses .scala configuration file instead of .sbt, so let’s add dependency into project/build.scala. Take Dojo for example.
1 | object DwExplorerBuild extends Build { |
To view this dependency in Eclipse, you need to run sbt eclipse again. In the Referenced Libraries section, you can see a dojo-1.9.3.jar, and the library lies in META-INF/resources/webjars/.
Add a Route for WebJars Resources
Find the ProjectNameStack.scala file and add the following lines at the bottom of the trait:
1 | trait ProjectNameStack extends ScalatraServlet with ScalateSupport { |
That’s it! Now you can refer to the WebJars resources in views, like this:
1 | #set (title) |
Some Explanations on This Route
/webjars/*is a Wildcards andparams("splat")is to extract the asterisk part.resourcePathpoints to the WebJars resources in the jar file, as we saw in Eclipse. It is then fetched as anInputStreamwithgetResourceAsStream().servletContext.getMimeType()is a handy method to determine the content type of the requested resource, instead of parsing it by ourselves. I find this in SpringMVC’s ResourceHttpRequestHandler.IOUtilis a utiliy class that comes with Scalate, so don’t forget to import it first.
At first I tried to figure out whether Scalatra provides a conveniet way to serve static files in classpath, I failed. So I decided to serve them by my own, and this gist was very helpful.
Anyway, I’ve spent more than half a day to solve this problem, and it turned out to be a very challenging yet interesting way to learn a new language, new framework, and new tools. Keep moving!