spring mvc中的静态资源映射

时间:2014-10-17 06:28:25

标签: jsp spring-mvc static-resource

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

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

我测试了这个,用于直接访问浏览器地址栏中url中的图像:        hhttp://主机:端口/ ManageDepartments /图像/ oracle.PNG

它在浏览器上呈现所请求的图像,但是为什么它会阻止spring控制器URL提供用户请求。在弹出上下文文件中映射图像的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" />    

以下是项目结构:

enter image description here

任何建议都非常感谢!

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

1 个答案:

答案 0 :(得分:0)

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

但在您的使用案例中,您的资源不是直接在root下,并且应该足以在DispatcherServlet文件中将/*映射到/而不是web.xml