【问题标题】:static resource mapping in spring mvcspring mvc中的静态资源映射
【发布时间】:2014-10-17 22:12:00
【问题描述】:

我在 spring 上下文文件中对图像文件进行了映射,如下所示:

<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />

我对此进行了测试,以直接访问浏览器地址栏中 url 中的图像: hhttp://host:port/ManageDepartments/images/oracle.PNG

它在浏览器上呈现请求的图像,但为什么它阻止弹簧控制器 url 为用户请求提供服务。在 spring 上下文文件中对图像进行 url 映射后,当我在 url 下方尝试时,它返回 404 错误。

http://host:port/ManageDepartments/department/

在上下文文件中进行图像映射后无法获取部门主页!

对于jsp,下面是spring配置文件中的配置:

    <bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />    

以下是项目结构:

非常感谢任何建议!

Web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

  <display-name>Spring With MongoDB Web Application</display-name>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

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

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

</web-app>

调度员

<?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:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.2.xsd">

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

<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />

<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="host" value="localhost" />
</bean>

<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="test" />
</bean>

<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


<bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />    

</beans>

在控制台中,我没有看到任何错误,但是在浏览器中,当我点击 hhttp://host:port/ManageDepartments/department/ 时,我得到了 HTTP 状态 404,这在获取部门主页之前是有效的。

Rgrds

【问题讨论】:

  • 显示你的 web.xml 和 dispatcher 。你有没有在控制台中发现任何错误>
  • @SanKrish 请查找更新后的帖子!
  • 我最近写了一个关于类似问题的答案here
  • @SergeBallesta 仍然没有得到解决方案。可以通过调度程序 servlet url 模式“\”访问控制器,或者如果我在 spring 配置文件中创建 url 映射条目,则可以访问图像!

标签: jsp spring-mvc static-resource


【解决方案1】:

正如我在this other post 中解释的那样,没有办法让 spring-mvc 允许直接在根目录下提供资源将根 URL 映射到控制器。

但是在您的用例中,您的资源不是直接在根目录下,将DispatcherServlet 映射到/* 而不是/ 文件中的/ 就足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-13
    • 2015-02-26
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多