如何在Spring MVC中使用CDN

4
我想在使用Spring MVC创建的项目中使用CDN来提供静态内容,如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中获取参数?

请帮帮我,谢谢。


在控制器中,您可以执行request.setAttribute("env.cdnUrl", <value>)。 - anurag gupta
创建一个过滤器并添加到其中;让所有控制器都通过它们。 - anurag gupta
好的,如果已配置,您还可以在.properties文件中设置env.cdnUrl并访问相同的内容。 - anurag gupta
@anuraggupta 在哪个属性文件中?你能多告诉我一些吗?谢谢。 - Haozhe Xie
你的applicationContext文件使用了任何属性文件吗?例如使用方法是http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/。 - anurag gupta
显示剩余3条评论
3个回答

3

我使用以下步骤在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" />

祝您好运!


也许你可以在我的博客文章中获取更多信息。 - Haozhe Xie

0

总结的方法:

  1. 在控制器中使用request.setAttribute("env", ),并在jsp中访问相同的内容。
  2. 创建一个servlet过滤器,并执行上述描述的相同操作。
  3. 将env值写入属性文件,并尝试在jsp页面中访问该值。如果使用InternalResourceViewResolver,则exposedContextBeanNames可以帮助在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> 
    

并且可以在 JSP 中通过 ${properties.env} 访问这些值。


这是我的PropertiesFactoryBean <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:/app.properties</value> </list> </property> </bean> 修改后,我无法在Spring配置文件中访问值。 - Haozhe Xie
你在视图解析器中也添加了 exposedContextBeanNames 吗? - anurag gupta
是的。而且我没有在JSP中测试它是否有效。因为我无法从Spring配置文件中加载属性。应用程序加载失败。 - Haozhe Xie
由于:java.lang.ClassNotFoundException: ${properties.jdbc.driverClassName} - Haozhe Xie
仍然无法工作:Caused by: java.lang.ClassNotFoundException: ${jdbc.driverClassName}。我确定在配置文件中有jdbc.driverClassName=com.mysql.jdbc.Driver - Haozhe Xie
显示剩余2条评论

0

你也可以通过拦截器来实现这个功能。

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

然后在拦截器代码中,您可以设置该属性

@Override
public boolean preHandle(final HttpServletRequest request, 
 final HttpServletResponse response, 
  final Object handler) {
 // set the attribute for URL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接