【问题标题】:How to use CDN in Spring MVC如何在 Spring MVC 中使用 CDN
【发布时间】:2015-02-26 03:56:54
【问题描述】:

我想在使用 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中获取参数?

请帮助我。谢谢。

【问题讨论】:

  • 你可以在控制器中做 request.setAttribute("env.cdnUrl", ) 。
  • @anuraggupta 但我必须在每个控制器中声明它。
  • 创建一个过滤器并在那里添加;让所有控制器通过它们
  • @anuraggupta 你的意思是使用AOP?有更简单的方法吗?

标签: java spring-mvc jsp cdn staticresource


【解决方案1】:

我在 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" />

祝你好运!

【讨论】:

  • 也许你可以在我的blog post获得更多信息。
【解决方案2】:

方法总结:

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

并以 ${properties.env} 访问 jsp 中的值

【讨论】:

  • 这是我的 PropertiesFactoryBean &lt;bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:/app.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; 改成这个后,我无法访问 Spring 配置文件中的值。
  • 你是否也在 viewResolver 中添加了exposedContextBeanNames ?
  • 是的。而且我没有测试它在 JSP 中是否有效。因为我无法从 Spring config 的属性文件中加载属性。应用加载失败。
  • 引起:java.lang.ClassNotFoundException: ${properties.jdbc.driverClassName}
  • 如果在属性文件中你有 "jdbc.driverClassName" 作为键然后写 ${jdbc.driverClassName}
【解决方案3】:

您也可以通过拦截器完成此操作。

  <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

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 2014-06-10
    • 2016-02-15
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多