如何在Spring MVC中使用CDN [英] How to use CDN in Spring MVC

查看:57
本文介绍了如何在Spring MVC中使用CDN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个用Spring MVC创建的项目中使用CDN来提供静态内容,如CSS、JavaScript和图片。但我不知道怎么做。

我是Spring的新手,我在网上看到过一些帖子:

但他们没有解释如何实施。

例如:

过去,我使用<c:url>标签:

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

当我使用CDN时,我可能会使用以下代码:

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

但是我应该把${env.cdnUrl}放在哪里(在web.xmldispatcher-servlet.xml(Spring MVC的配置)中)?如何在JSp中获取参数?

请帮帮我。谢谢。

推荐答案

我在Spring中实现了CDN服务,步骤如下:

dispatcher-servlet.xml(您的Spring配置)

中添加以下行
<util:properties id="propertyConfigurer" location="classpath:/app.properties"/>
<context:property-placeholder properties-ref="propertyConfigurer" />

当然,您需要在文件的顶部添加spring-util的DOM:

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"

安装位置app.properties

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

在JSP文件中使用CDN

<%@ 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" />

祝你好运!

这篇关于如何在Spring MVC中使用CDN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆