Eclipse:元素类型“上下文”必须声明

时间:2019-07-11 08:58:15

标签: java xml eclipse tomcat context.xml

我正在Eclipse上使用“ context.xml”与JDBC建立连接池。 它继续说 -必须声明元素类型“资源”。 -必须声明元素类型“上下文”。 -必须声明元素类型“ WatchedResource”。

尽管如此,同一代码昨天仍然运行良好。 我刚刚在学校的PC上导入了该项目。 所有其他不使用连接池的文件仍然可以正常运行。 我使用Tomcat 9.0版

我复制了昨天输入的代码,并将其保存为文本文件。 我删除了“ context.xml”文件,然后重新创建。 它仍然不起作用。 我在google上搜索并叠加了Over Flow,以找出是否有人遇到相同的问题,很遗憾,我找不到任何答案。

请问有人可以帮我吗?

这是我键入的xml代码。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE context>
<Context path="/" docBase="Webprj" reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource
name="jdbc/Oracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
username="sijeune" password="oracle"
maxActive="20" maxIdle="10" maxWait="-1"/>
</Context>

Error messages appear here

我的xml文件的根是 'C:\ Users \ 1027 \ Java \ GroupStudy \ Webprj \ WebContent \ META-INF \ context.xml'

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道您为什么在项目中分别创建context.xml

Tomcat context.xml目录中已经有一个conf文件。只需将数据源添加到context.xml

来自Tomcat 9 JNDI-Datasource Documentation

只需在其中添加数据源。

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    <!-- The contents of this file will be loaded for each web application -->
    <Context>

        <!-- Default set of monitored resources. If one of these changes, the    -->
        <!-- web application will be reloaded.                                   -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->

    <Resource name="jdbc/myoracle" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
              username="sijeune" password="oracle" maxTotal="20" maxIdle="10"
              maxWaitMillis="-1"/>
    </Context>

web.xml

只需添加以下内容:

<resource-ref>
 <description>Oracle Datasource example</description>
 <res-ref-name>jdbc/myoracle</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>