4

I want to use CDN to serve static content like CSS, JavaScript and images in a project created with Spring MVC. But I didn't how to do it.

I'm new to Spring and I have seen some posts on the web:

But they didn't explain how to implement it.

For example:

In the past, I use <c:url> tags:

<img src="<c:url value="/path/to/image" />" alt="desc" />

When I use CDN, I may use following code:

<img src="${env.cdnUrl}/mypath/pic.jpg" />

But where should I put ${env.cdnUrl}(in web.xml or dispatcher-servlet.xml(the configuration of Spring MVC))? And how to get the parameter in JSP?

Please help me. Thanks.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
Haozhe Xie
  • 3,438
  • 7
  • 27
  • 53

3 Answers3

3

I implemented CDN service in Spring using following steps:

Add following lines in dispatcher-servlet.xml (Your Spring Configuration)

<util:properties id="propertyConfigurer" location="classpath:/app.properties"/>
<context:property-placeholder properties-ref="propertyConfigurer" />

Of course, you need to add DOM for spring-util at the top of the file:

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

Setup in app.properties

cdn.url=//cdn.domain.com/path/to/static/content

Use CDN in JSP files

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:eval expression="@propertyConfigurer.getProperty('cdn.url')" var="cdnUrl" />

<link rel="stylesheet" type="text/css" href="${cdnUrl}/css/semantic.min.css" />
<link rel="stylesheet" type="text/css" href="${cdnUrl}/css/font-awesome.min.css" />

Good luck!

Haozhe Xie
  • 3,438
  • 7
  • 27
  • 53
  • Maybe you can get more information in my [blog post](http://infinitescript.com/2015/04/use-cdn-service-in-spring-mvc/). – Haozhe Xie Dec 18 '15 at 01:22
0

approaches summarized:

  1. use request.setAttribute("env", ) in controller and access the same in jsp.
  2. create a servlet filter and do the same activity described above.
  3. write the env value to properties file and try to access that in jsp pages. if using InternalResourceViewResolver then exposedContextBeanNames can help with exposing properties in jsp.

    <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
    <list><value>property_file</value></list>
    </property>
    </bean>
    
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass"     value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/> 
    <property name="exposeContextBeansAsAttributes" value="true"/> 
    <property name="exposedContextBeanNames">
        <list>  
            <value>properties</value> 
        </list>
    </property>  
    </bean> 
    

and access the values in jsp as ${properties.env}

anurag gupta
  • 379
  • 1
  • 5
  • Here's my PropertiesFactoryBean ` classpath:/app.properties ` After change to this, I can't access value in Spring configuration files. – Haozhe Xie Feb 26 '15 at 07:23
  • did you added exposedContextBeanNames in viewResolver as well ? – anurag gupta Feb 26 '15 at 07:25
  • Yes. And I didn't test whether it works or not in JSP. Because I can't load the properties from the properties file in Spring config. The application failed to load. – Haozhe Xie Feb 26 '15 at 07:27
  • Caused by: java.lang.ClassNotFoundException: ${properties.jdbc.driverClassName} – Haozhe Xie Feb 26 '15 at 07:30
  • if in a properties file you have "jdbc.driverClassName" as key then write ${jdbc.driverClassName} – anurag gupta Feb 26 '15 at 07:32
  • Still not working: `Caused by: java.lang.ClassNotFoundException: ${jdbc.driverClassName}`. And I'm sure I have `jdbc.driverClassName=com.mysql.jdbc.Driver` in configuration file. – Haozhe Xie Feb 26 '15 at 07:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71745/discussion-between-anurag-gupta-and-howard-shieh). – anurag gupta Feb 26 '15 at 07:35
0

You could also accomplish this via an interceptor.

  <mvc:interceptors>
            <!-- path interceptor adds servlet path as an attribute -->
        <bean class="com.test.myInterceptor" />

Then in the interceptor code, you can have the attribute be set

@Override
public boolean preHandle(final HttpServletRequest request, 
 final HttpServletResponse response, 
  final Object handler) {
 // set the attribute for URL
Paul John
  • 1,626
  • 1
  • 13
  • 15