【问题标题】:Spring MVC project local resourceSpring MVC 项目本地资源
【发布时间】:2026-01-10 10:45:01
【问题描述】:

我正在尝试在我的项目中实施this。我将在C:\resource\pdf\ 中拥有本地资源。

更新:

我之前的配置运行良好:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.neu.als.thesis.web.controllers" />

  <!--  Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
  </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="10000000"/>
  </bean>

</beans>

并修改它以实现本地资源到这个:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  <context:component-scan base-package="com.neu.als.thesis.web.controllers" />

  <mvc:resources mapping="/picture/**" location="file:/resource/" />

  <!--  Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
  </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="10000000"/>
  </bean>

</beans>

但是当我运行项目时,会抛出 404 错误。我控制台的最后几行是:

Nov 02, 2013 8:12:21 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/ThesisProject/] in DispatcherServlet with name 'ThesisProject'

似乎无法找到默认控制器。我错过了什么?

更新 2

这是我在 web.xml 中的 servlet 定义

<!-- Servlet definition -->
  <servlet>
      <servlet-name>ThesisProject</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>ThesisProject</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/ThesisProject-servlet.xml</param-value>
  </context-param>

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

【问题讨论】:

  • 问题仍未解决。

标签: java spring spring-mvc tomcat7


【解决方案1】:

你可以试试

 mvc:resources mapping="/picture/**" location="file:///C:/resource/pdf" />

请求您的资源,例如

 servlermappingmvc/picture/pdf/mypdf.pdf

更新

您可以将 servlet-mapping .do 更改为 /controller/

【讨论】:

  • 对不起,如果我混淆了你。我目前的问题是由于我在配置中所做的修改,无法检测到我的默认控制器。
  • 好的,但是你可以分享你的web.xml和其他配置资源
  • 如果只添加 mvc:resources 我认为找不到资源文件夹。你可以试试我的答案吗?
  • 我运行该项目得到了404,还尝试通过http://localhost:8080/ThesisProject/picture/pdf/test.pdf 访问,但仍然是404 异常
  • 您可以尝试将您的 ThesisProject servlet-mapping 从 asterasterisk.do 更改为 /controller/asterisk,然后使用 localhost:8080/ThesisProject/controller/picture/pdf/test.pdf 尝试您的 pdf。您的资源配置在您的控制器 Servlet 上,但您的请求与 *.do 不匹配
【解决方案2】:
Nov 02, 2013 8:12:21 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/ThesisProject/] in DispatcherServlet with name 'ThesisProject'

您是否已将应用程序部署在 Web 容器的根上下文路径中?

您可以尝试使用http://localhost:8080/picture/pdf/test.pdf 下载文件吗?

【讨论】: