spring mvc:上下文根目录之外的资源路径

时间:2012-09-11 14:03:33

标签: spring spring-mvc

我已经在stackoverflow上看到了另一个解决方案,但它没有帮助。我做同样的事情,但我不知道为什么它不适合我。

我正在ubuntu机器上的/ home / images文件夹中上传图片,在spring-servlet.xml中我写了以下几行

  <mvc:resources mapping="/resources/**" location="/resources/" /> 

<mvc:resources mapping="/images/**" location="file:/home/images/"/>
<mvc:default-servlet-handler/>

图片上传到/ home / images /文件夹但我无法访问这些图片

在JSP中我写过

   <img src="/images/image.jpg"/>

但它没有显示这个图像我不明白这里的问题。如果需要其他任何内容,请告诉我。

---更新---

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">



<context:annotation-config />   

<context:component-scan
    base-package="com.mycom.myproject" />

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" /> 

<mvc:resources mapping="/images/**" location="file:/home/images/"/>
<mvc:default-servlet-handler/>



<!-- Declare a datasource that has pooling capabilities -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost/dbtest" p:user="root" p:password="root"
p:acquireIncrement="10" p:idleConnectionTestPeriod="60" p:maxPoolSize="100"
p:maxStatements="50" p:minPoolSize="10" />
  
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="datasource" />


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="datasource" />
</bean>

<!-- scan for mappers and will automatically scan the whole classpath for xmls -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    <property name="basePackage" value="com.mycom.myproject.db.mybatis.dao" />
</bean>  

<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean> 

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"
   p:extractValueFromSingleKeyModel="true" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:project-config" />
</bean>

Web.xml中

 <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-servlet.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>


<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 <session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

1 个答案:

答案 0 :(得分:3)

oooook,你的调度程序servlet被映射到 .htm,所以你的调度程序servlet永远不会被调用,因为它处理那些/ resources / *请求并调用ResourceHttpRequestHandler来编写静态内容。