Spring 3.1.1 SAXParseException с mvc: ресурсы

Я получаю следующую ошибку в своих журналах:

Причина: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: соответствующий подстановочный знак является строгим, но для элемента «mvc:resources» не найдено объявление.

Это появляется, когда я пытаюсь просмотреть свое приложение в браузере:

Ошибка 500: javax.servlet.ServletException: SRVE0207E: Неперехваченное исключение инициализации, созданное сервлетом

Вот мой web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    id="WebApp_ID"
    version="2.5"
    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
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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/spring-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="org.myapp.controllers" />
    <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/views/" />
        <property
            name="suffix"
            value=".jsp" />
    </bean>
    <mvc:resources
        mapping="/resources/**"
        location="/resources/" />
</beans>
5.xsd"> <display-name>MyAwesomeApp</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

Вот мой spring-servlet.xml:

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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/spring-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="org.myapp.controllers" />
    <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/views/" />
        <property
            name="suffix"
            value=".jsp" />
    </bean>
    <mvc:resources
        mapping="/resources/**"
        location="/resources/" />
</beans>

Что я делаю неправильно? Если я уберу тег <mvc:resources, мое приложение появится, но его CSS не загрузится.

Редактировать: возможно, у меня есть какие-то другие проблемы, потому что теперь я не получаю эту ошибку, хотя мое приложение не отображается — я просто получаю 404. Я получаю это в log сейчас, что выглядит многообещающе:

SimpleUrlHand I org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler Сопоставлен URL-путь [/resources/**] с обработчиком 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'


person Sarah Vessels    schedule 27.03.2012    source источник
comment
Первое исключение, которое вы публикуете, предполагает, что существует проблема при анализе файла xml spring-servlet.xml   -  person nicholas.hauschild    schedule 28.03.2012


Ответы (2)


Вы допустили ошибку в объявлении пространств имен в spring-servlet.xml. Пожалуйста измените:

xsi:schemaLocation="
(...)
http://www.springframework.org/schema/mvc/spring-mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
(...)"

to:

xsi:schemaLocation="
(...)
http://www.springframework.org/schema/mvc/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
(...)"

Поскольку вы объявили

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

не (...)/schema/spring-mvc.

person omnomnom    schedule 27.03.2012

В итоге я последовал этому руководству, создавая новый проект шаблона Spring с использованием шаблона проекта Spring MVC. Затем я создал EAR (новый проект корпоративного приложения в Rational Application Developer), который включал мой новый проект Spring MVC. Развернул EAR на моем WAS 7, все в порядке. Я помещаю свои файлы CSS и JavaScript в src/main/webapp/resources и ссылаюсь на них в своих представлениях с помощью resources/stylesheet.css. Проект шаблона поставляется с уже настроенным каталогом ресурсов.

Я заметил, что его файл контекста ссылается на http://www.springframework.org/schema/mvc как в xmlns, так и в xsi:schemaLocation, а не на http://www.springframework.org/schema/mvc/spring-mvc, как я делал в xsi:schemaLocation, поэтому я думаю, что Piotrek ответ, вероятно, правильный.

person Sarah Vessels    schedule 27.03.2012