【问题标题】:Mapping to external image folder not working (<mvc:resources>)映射到外部图像文件夹不起作用 (<mvc:resources>)
【发布时间】:2013-03-18 14:45:38
【问题描述】:

我想将我的图像外包到 spring 项目文件夹之外。经过一番研究,我发现了 mvc:resources 标签,这似乎是满足我要求的完美解决方案。

app-servlet.xml

xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven/>
<mvc:resources mapping="/pics/**" location="file:/c:/Tomcat_6/webapps/external_resources/" order="0" />

JSP 调用:

<img src="<c:url value="/pics/test.png"/>" />

我不知道为什么这对我不起作用。

几个小时后,我读到删除以下行将解决问题,但什么也没发生。

<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property value="true" name="alwaysUseFullPath"></property>
    <property name="defaultHandler">    
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </property>
    <property name="order" value="1"/>
</bean>

也在变化

<servlet-mapping>
    <servlet-name>onlinecatalog</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>onlinecatalog</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

没有帮助。

【问题讨论】:

    标签: spring-mvc resources servlet-mapping


    【解决方案1】:

    您可以像这样使用基于 java 的配置(在 linux 中)

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String rootPath = System.getProperty("user.home"); 
    String imagePath = "file:"+rootPath + File.separator + "tmpFiles/"; //absolute path of the directory
    System.out.println(imagePath);
    registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
    registry.addResourceHandler("/tmpFiles/**").addResourceLocations(imagePath);
    

    }

    现在您可以从 tmpFiles 文件夹访问外部文件。 在 Windows 中,您可能需要您的 imagePath 作为

    imagePath="file:c:/Tomcat_6/webapps/external_resources/";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2014-12-01
      • 2015-12-05
      • 1970-01-01
      • 2011-11-05
      • 2014-04-21
      相关资源
      最近更新 更多