映射到外部图像文件夹不起作用(<mvc:resources>)



我想将我的图像外包到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>

您可以像这样使用基于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/";

最新更新