0

我在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,该页面之前用于获取部门主页。

数据库

4

1 回答 1

0

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

但是在您的用例中,您的资源并不直接在根目录下,它应该足以映射DispatcherServlet/*而不是/在您的web.xml文件中。

于 2014-10-17T14:12:00.053 回答