小编典典

javax.naming.NamingException,Tomcat7和Struts2无法通过JNDI访问数据库资源

tomcat

我正在使用Struts2框架(eclipse)开发新的Web应用程序,并且已经使用Hibernate映射了数据库结构。问题是当我执行“列表”操作时,框架会向我返回此错误:

    支柱问题报告

    Struts已检测到未处理的异常:

    讯息:   
    必须通过java:URL访问此上下文。
    无法查找JNDI名称[java:comp / env / jdbc / main]
    文件:org / apache / naming / SelectorContext.java
    行号:776

server.xml是: ``

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
    <Listener className="org.apache.catalina.core.JasperListener"/>
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

    <GlobalNamingResources>
        <Resource auth="Container" defaultAutoCommit="false" driverClassName="net.sourceforge.jtds.jdbc.Driver" maxActive="200" maxIdle="100" maxWait="20000" name="jdbc/main" password="########" type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://10.4.1.85/SBAGRU_DEV" username="****"/>

    </GlobalNamingResources>
    <Service name="Catalina">
        <Connector connectionTimeout="2000" executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

        <Connector SSLEnabled="true" clientAuth="false" keystoreFile="C:\jpeople.bin" keystorePass="gesbanke" keystoreType="PKCS12" maxThreads="200" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="SSL"/>

        <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> -->
        <Engine defaultHost="localhost" name="Catalina">
            <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false"> 
                <Context docBase="mag0" path="/mag0" reloadable="true" source="org.eclipse.jst.jee.server:mag0">
                    <ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" />
                </Context>
            </Host>
        </Engine>
    </Service>
</Server>

我也有hibernate.cfg.xml

``

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
    <Listener className="org.apache.catalina.core.JasperListener"/>
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

    <GlobalNamingResources>
        <Resource auth="Container" defaultAutoCommit="false" driverClassName="net.sourceforge.jtds.jdbc.Driver" maxActive="200" maxIdle="100" maxWait="20000" name="jdbc/main" password="########" type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://10.4.1.85/SBAGRU_DEV" username="****"/>

    </GlobalNamingResources>
    <Service name="Catalina">
        <Connector connectionTimeout="2000" executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

        <Connector SSLEnabled="true" clientAuth="false" keystoreFile="C:\jpeople.bin" keystorePass="******" keystoreType="PKCS12" maxThreads="200" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="SSL"/>

        <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> -->
        <Engine defaultHost="localhost" name="Catalina">
            <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false"> 
                <Context docBase="mag0" path="/mag0" reloadable="true" source="org.eclipse.jst.jee.server:mag0">
                    <ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" />
                </Context>
            </Host>
        </Engine>
    </Service>
</Server>

有什么建议可以看吗?我真的快要疯了!


阅读 365

收藏
2020-06-16

共1个答案

小编典典

我找到了 解决方案真正的问题 。我在 Eclipse中 编辑 server.xml
,但没有看到真正的发布结果。出于奇怪的原因, eclipse 删除了真实 server.xml 中的资源链接,但在 eclipse
视图中向我展示了该链接。因此,我整日忙于搜索,发现必须在 WEB-INF 目录下的 context.xml 文件中添加以下内容: __


<?xml version="1.0" encoding="UTF-8"?> 
<Context antiJARLocking="true" path="">
<ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" /> 
</Context>
2020-06-16