Jawr, webjars, bootstrap, Spring setup trick

It is well known that you could not escape from some kind web optimizer library when developing web applications.

In case of Java, one of the best tool is called Jawr. Extremely flexible and easy to use.

The other set of tools which helps to keep you project clean from third party JavaScript libraries is the webjars. Luckily Jawr is providing support for using resources from classpath.

I have run into one serious problem only: when using some non js, css or image resources I could not access throug Jawr. More precisely I could not access glyphicons from bootstrap webjar. Jawr is rewriting css url reference to something like this:

1url('../jar:/META-INF/resources/webjars/bootstrap/3.1.1/fonts/glyphicons-halflings-regular.ttf')

Here it is the trick to resolve this non existing path (as Jawr is not serving font files…).

In the web.xml you must tell Spring to watch for jar: prefixed paths:

 1  <servlet>
 2    <servlet-name>SpringDispatcherServlet</servlet-name>
 3    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4    <init-param>
 5      <param-name>contextConfigLocation</param-name>
 6      <param-value>classpath:spring-mvc.xml</param-value>
 7    </init-param>
 8    <load-on-startup>1</load-on-startup>
 9  </servlet>
10  <servlet-mapping>
11    <servlet-name>SpringDispatcherServlet</servlet-name>
12    <url-pattern>/app/*</url-pattern>
13    <!-- for webjar resources see spring-mvc.xml -->
14    <url-pattern>/jar:/*</url-pattern>
15  </servlet-mapping>

In spring mvc config we should map the resource to the proper classpath:

1  <mvc:resources mapping="/META-INF/resources/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

And enjoy the result.


Technology stack
May 16, 2014

Links

Cool

RSS