47

这是我的web.xmlxsd

<?xml version="1.0" encoding="UTF-8"?>
    <web-app 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_3_0.xsd"
    version="3.0">

这是servlet节点

<servlet>
    <servlet-name>spring1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param> <!-- here is a problem -->
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
</servlet>

在标记的行 xml 验证器上说

cvc-complex-type.2.4.a:发现以元素“init-param”开头的无效内容。'{" http://java.sun.com/xml/ns/javaee ":enabled, " http://java.sun.com/xml/ns/javaee ":async-supported, " http:// /java.sun.com/xml/ns/javaee ":run-as, " http://java.sun.com/xml/ns/javaee ":security-role-ref, " http://java.sun .com/xml/ns/javaee ":multipart-config}' 是预期的。

出了什么问题,我该如何纠正这个错误?

4

3 回答 3

136

web.xml在我遇到的所有示例中,元素的顺序<load-on-startup>都是<init-param>.

<servlet>
    <servlet-name>spring1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
于 2011-03-03T00:15:30.377 回答
17

这是迂腐的,但<init-param>必须先来<load-on-startup>,所以:

<servlet>
    <servlet-name>spring1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param><!--here is a problem-->
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
于 2011-03-03T00:13:21.343 回答
0

按照这个配置

  <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

      <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet-context.xml</param-value>
        </context-param>

        <servlet>
            <servlet-name>dispatcher-servlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

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

    </web-app>
于 2020-02-06T09:58:27.043 回答