【问题标题】:Spring MVC Schema giving SAXParseExceptionSpring MVC Schema 给出 SAXParseException
【发布时间】:2015-03-19 12:53:32
【问题描述】:

当我尝试在dispatcher-servlet.xml 中添加spring-mvc 架构时,我收到以下错误。我从 STS 中给出的 spring 示例中复制了 xml 文件。我正在使用 Spring 4.1.1。仅在使用 mvc 架构时才会出现此问题,而不会出现其他任何问题:

org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 116; Error:  cos-all-limited.1.2:  The minOccurs attribute of a model group with "all" compositor that is part of a pair that is the content type of a complex type definition must have the value one.  The value "0" is incorrect.

我的文件是:

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <annotation-driven />
    <resources mapping="/resources/**" location="/resources/" />
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/jsp/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>       
    <context:component-scan base-package="com.mj.cchp" />    
</beans:beans>

【问题讨论】:

  • 您提供的错误和架构似乎无关紧要。你确定吗?
  • @Keerthivasan 是的。仅当我包含 mvc 架构时才会发生此错误,否则不会发生此错误。
  • 导入的 XSD 架构文件可能有问题。版本冲突。我认为您需要在schemaLocation 属性中提供适当版本的 XSD 的 URL。请检查一下
  • @Keerthivasan 不,没有必要。如果没有给出版本,它会自动获取最新版本。另外,我已经尝试提供版本,但仍然出现相同的错误。
  • 哦,好的,谢谢您告诉我。我也不确定发生了什么。

标签: java xml spring spring-mvc


【解决方案1】:

没有命名空间作为“资源”。使用以下标签,

 <mvc:resources mapping="/resources/**" location="/resources/"/>

要使这个工作,请添加 mvc 命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans 
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 ....
 xsi:schemaLocation="
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 ......

【讨论】:

【解决方案2】:
<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.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mj.cchp" /> 
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>          
</beans>

InternalResourceViewResolver是解析WEB-INF等受保护文件夹下的资源。

试试上面的。将beans 设为default namespace。两者都应该工作,但这对我来说适用于 Spring-4.1.1

【讨论】:

  • 没有版本的架构更好。 Spring 指向最新版本的 xsds。如果 xsd 不在 jars 中或 jars 不在类路径中,则可能会发生这种情况。所以清理你的项目并使用最新的 jars 运行。或者有时 spring 可能会使用另一个版本而不是 4.1(谁知道)。获取最新的 jars,或者如果这是一个 maven 项目,请删除 spring jars 并再次更新。
猜你喜欢
  • 2015-01-03
  • 2015-08-15
  • 2014-07-31
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2012-06-02
相关资源
最近更新 更多