【问题标题】:Spring 3.1.1 SAXParseException with mvc:resourcesSpring 3.1.1 SAXParseException 与 mvc:resources
【发布时间】:2012-03-27 20:08:33
【问题描述】:

我的日志中出现以下错误:

原因:org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 匹配的通配符是严格的,但找不到元素“mvc:resources”的声明。

当我尝试在浏览器中查看我的应用程序时会出现这种情况:

错误 500:javax.servlet.ServletException:SRVE0207E:未捕获的由 servlet 创建的初始化异常

这是我的 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_2_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>

我做错了什么?如果我取出 &lt;mvc:resources 标签,我的应用会显示出来,但它的 CSS 不会加载。

编辑:也许我还有一些其他问题,因为现在我没有收到那个错误,虽然我的应用程序没有显示——我只是得到一个 404。我确实得到了现在在日志中,看起来很有希望:

SimpleUrlHand I org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 映射 URL 路径 [/resources/**] 到处理程序 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'

【问题讨论】:

  • 您发布的第一个异常表明解析xml文件存在问题spring-servlet.xml

标签: java spring spring-mvc rad websphere-7


【解决方案1】:

你在spring-servlet.xml 的命名空间声明中犯了一个错误。 请更改:

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

到:

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

【讨论】:

    【解决方案2】:

    我最终关注了this tutorial,使用 Spring MVC 项目模板创建了一个新的 Spring 模板项目。然后,我创建了一个 EAR(Rational Application Developer 中的新企业应用程序项目),其中包括我的新 Spring MVC 项目。将 EAR 部署到我的 WAS 7,一切都很好。我将我的 CSS 和 JavaScript 文件放在 src/main/webapp/resources 中,并在我的视图中使用 resources/stylesheet.css 链接到它们。模板项目带有一个已经设置好的资源目录。

    我确实注意到它的上下文文件在 xmlns 和 xsi:schemaLocation 中都引用了 http://www.springframework.org/schema/mvc,而不是像我在 xsi:schemaLocation 中所做的那样 http://www.springframework.org/schema/mvc/spring-mvc,所以我认为 Piotrek's answer 可能是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2014-07-31
      • 1970-01-01
      • 2013-01-29
      • 2012-01-01
      相关资源
      最近更新 更多