我想使用 CDN 在使用 Spring MVC 创建的项目中提供静态内容,如 CSS、JavaScript 和图像。但是我不知道怎么做。
我是 Spring 的新手,我在网上看到了一些帖子:
- > JSP/Spring MVC and CDN?
- > How to use property from property file specified in PropertyPlaceholderConfigurer in JSP
- > How to show values from property file in JSP in a spring MVC app
- > http://tshikatshikaaa.blogspot.com/2012/11/serving-static-resources-with-spring-mvc.html
但他们没有解释如何实现它。
例如:
过去,我使用<c:url>
标签:
<img src="<c:url value="/path/to/image" />" alt="desc" />
当我使用 CDN 时,我可能会使用以下代码:
<img src="${env.cdnUrl}/mypath/pic.jpg" />
但是我应该把${env.cdnUrl}
放在哪里呢? (在web.xml
或dispatcher-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" />
祝你好运!
关于java - 如何在 Spring MVC 中使用 CDN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734196/