Jawr includes a Spring MVC Controller implementation which you can use instead of the usual Jawr servlet. This way, you can keep all your configuration in a single consistent format and benefit with the incredibly flexible configuration system that ships with Spring. The version supported is the 2.0.x version of spring MVC
Note that if you are new to Jawr, reading this guide alone will not suffice to understand how Jawr works, so do check out the rest of the documentation because here you will only find the instructions to plug it into Spring.
The first thing to do is to add the jawr-spring-extension module in your classpath. For maven users, you’ll need to add the following dependency in your pom file :
<dependency> <groupId>net.jawr.extensions</groupId> <artifactId>jawr-spring-extension</artifactId> <version>3.8</version> </dependency>
Assuming you have an up and running Spring web application, you will first need to add a bean definition for the class net.jawr.web.servlet.JawrSpringController. You will need an instance for each resource type (.js and .css) and a specific one for binary resources (.jpg, .png, ….), so it will be best if you declare an abstract bean and then you create three instances from it. Take a look at this full example and find all the details below:
<!-- Base Jawr config --> <bean abstract="true" id="jawrBase" class="net.jawr.web.servlet.JawrSpringController" > <!-- This must match the servlet-mapping to which the spring servlet is bound --> <property name="mapping" value="/spring/"/> <property name="configLocation" value="/jawr.properties" /> <property name="configuration"> <props> <prop>key="jawr.debug.on">false</prop> <prop key="jawr.gzip.on">true</prop> </props> </property> </bean> <!-- JS controller --> <bean id="jawrJsController" parent="jawrBase" /> <!-- CSS controller --> <bean id="jawrCSSController" parent="jawrBase"> <property name="type" value="css" /> </bean> <!-- Binary controller --> <bean id="jawrBinaryController" parent="jawrBase"> <property name="type" value="binary" /> </bean>
As you can see, the abstract bean holds most of the configuration details, which is common to both Jawr controllers. The properties which are being set configure the controllers in the following way:
There are two additional properties you might need to set:
Check the descriptor syntax page for an overview of what can be specified for the properties.
Once you have set up the controllers to your liking, you need to add mappings in the URL handler mapping bean of the servlet. The simplest and possibly best form to do it is to simply map all .js or .css requests to the controllers:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/**/*.js">jawrJsController</prop> <prop key="/**/*.css">jawrCSSController</prop> <prop key="/**/*.jpg">jawrBinaryController</prop> <prop key="/**/*.png">jawrBinaryController</prop> </props> </property> </bean>
If for some reason you need to add some kind of prefixing to the mappings, there is extra work to be done. For instance, if you mapped like this:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/jawr/**/*.js">jawrJsController</prop> <prop key="/jawr/**/*.css">jawrCSSController</prop> <prop key="/jawr/**/*.jpg">jawrBinaryController</prop> <prop key="/jawr/**/*.png">jawrBinaryController</prop> </props> </property> </bean>
For this configuration, you will need to let the controllers know the extra path information. The reason for this is that Jawr needs not only to attend to requests, but also to generate URLs which will match its own controllers (incidentally, that’s the same reason to need the mapping configuration attribute too). The controllerMapping configuration attribute comes into play under this configuration. You will need to set it with the value of the additional prefixing:
<!-- Base Jawr config --> <bean abstract="true" id="jawrBase" class="net.jawr.web.servlet.JawrSpringController" > <!-- This must match the servlet-mapping to which the spring servlet is bound --> <property name="mapping" value="/spring/"/> <!-- This must match the controller mapping prefix to which the jawr controllers are bound at the urlMapping bean --> <property name="controllerMapping" value="/jawr/"/> <property name="configuration"> <util:properties location="classpath:jawr.properties"/> </property> </bean>
With controllers in place and mappings set, you are ready to start using the Jawr tag library in your pages to get really fast loading times.
If you will be using Spring’s i18n facilities and want to generate 18n message scripts (check the 18n generator docs), you can integrate both systems by letting Jawr use Spring’s LocaleResolver. Jawr has its own way of resolving the locale of a request and a bit of extra configuration is needed to have it delegate in Spring. Actually, this single line in your jawr.properties file will do:
jawr.locale.resolver=net.jawr.web.resource.bundle.locale.SpringLocaleResolver