1

我通过 Spring mvc 制作了一个简单的 Web 应用程序。

我想使用这些网址

  • /用户
  • /用户身份}
  • /用户/创建
  • /user/edit/{id}

在 web.xml 中

第一种情况

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

它运作良好。
但我无法读取 http://localhost:8080/res/images/image.png
- {我的项目路径}/WebContent/res/images/logo.png 中的404 错误

第二种情况

<servlet-mapping> 
    <servlet-name>SpringMVC1</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

我可以在http://localhost:8080/res/images/image.png上看到图像, 但http://localhost:8080/user/create - 404 错误

怎么了??

4

2 回答 2

5

你的 XML 中需要这样的东西:

<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>

16.14.5。配置资源服务

于 2012-03-22T07:31:08.953 回答
4

更详细的解释..

在我的 spring 配置 xml 文件中

我追加

<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>

它必须在下一个附加..

附加到根节点 - bean

xmlns:mvc="http://www.springframework.org/schema/mvc"

并附加到 xsi:schemaLocation

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

并附加 mvc:annotation-driven 节点。

<mvc:annotation-driven />

这是我的弹簧配置 xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    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/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.0.xsd">
    <context:component-scan base-package="com.test" />
    <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>
    <mvc:annotation-driven />
    <mvc:resources mapping="/res/**" location="/res/" />
</beans>

它运作良好。
谢谢肖恩·帕特里克·弗洛伊德。

于 2012-03-23T06:15:12.080 回答