A few days ago, I created TestZilla on Aliyun Elastic Compute Service.

However, with the increment of PV, I decided to use CDN to cache static files(images, CSS, and javascript).

But there’s no information on how to use CDN with Spring MVC, so I asked a question on StackOverflow.

Setup Spring Configuration

First of all, you need to use PropertyPlaceholderConfigurer in Spring Configuration(such as dispatcher-servlet.xml)

<!-- Property File Location -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/testzilla.properties</value>
        </list>
    </property>
</bean>
<util:properties id="propertyConfigurer" location="classpath:/testzilla.properties"/>
<context:property-placeholder properties-ref="propertyConfigurer" />

Of course, you need to add XML Namespaces:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-4.1.xsd">
</beans>

Create Properties File

As I was declared in the Spring configuration, there is a file named testzilla.properties in the classpath.
You need to create the file and add the following lines to it:
[plain]cdn.url=//cdn.testzilla.org/[/plain]

Of course, you can add extra properties if needed:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testzilla?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=

Use CDN Property in JSP

Now, we can get the URL of CDN in JSP as follows:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:eval expression="@propertyConfigurer.getProperty('cdn.url')" var="cdnUrl" />
<!DOCTYPE html>
<html lang="${language}">
<head>
    <meta charset="UTF-8">
    <title>Home | TestZilla</title>
    <link rel="stylesheet" type="text/css" href="${cdnUrl}/css/semantic.min.css" />
</head>
</html>

Reference

The Disqus comment system is loading ...
If the message does not appear, please check your Disqus configuration.