Spring сопоставление URL-адресов MVC

Я сделал простое веб-приложение с помощью Spring mvc.

Я хочу использовать эти URL

  • /Пользователь
  • /Идентификатор пользователя}
  • /пользователь/создать
  • /пользователь/изменить/{идентификатор}

в веб.xml

первый случай

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

Он работает хорошо.
но я не могу прочитать http://localhost:8080/res/images/image.png — ошибка 404
в {моем пути к проекту}/WebContent/res/images/logo.png

второй случай

<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

Что случилось??


person Jaime    schedule 22.03.2012    source источник


Ответы (2)


Вам нужно что-то вроде этого в вашем XML:

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

См. 16.14. 5. Настройка обслуживания ресурсов

person Sean Patrick Floyd    schedule 22.03.2012

подробнее поясните..

в моем XML-файле конфигурации весны

я добавляю

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

это должно быть добавлено далее..

добавить к корневому узлу - beans

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>

Это работает хорошо.
Спасибо, Шон Патрик Флойд.

person Jaime    schedule 23.03.2012