java - 如何在 Spring MVC 中使用 CDN

标签 java spring-mvc jsp cdn staticresource

我想使用 CDN 在使用 Spring MVC 创建的项目中提供静态内容,如 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" />

祝你好运!

关于java - 如何在 Spring MVC 中使用 CDN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734196/

相关文章:

java - 找到缓慢 JSP 根本原因的最佳技术是什么?

java - 数组组合的子集 - AS3

java - spring-nullpointerexception- 无法访问无注释类中的 Autowiring 注释服务(或 dao)

java - 在 Eclipse 控制台中包含与 System.out.print 语句对应的文件名/行号

java - 我的 BeanPostProcessor 在 Spring Boot 中不起作用

java - 在 Java 中上传文件时重置 InputStream

java - 我该如何解决JSP中的这段代码?

javascript - 我的 html 代码的 JSP 部分未在浏览器上显示

java - 如何停用 Spring Data 异常转换

java - Spring MVC - 将值从 jsp 传递到 Controller - HTTP 状态 400 - 客户端发送的请求在语法上不正确