我正在使用Struts2框架开发新的Web应用程序,eclipse,并且我已经使用Hibernate映射了db结构。问题是,当我执行“list”操作时,框架会返回此错误:
Struts Problem Report Struts has detected an unhandled exception: Messages: This context must be accessed through a java: URL Unable to lookup JNDI name [java:comp/env/jdbc/main] File: org/apache/naming/SelectorContext.java Line number: 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>
<?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>
我有什么建议吗?我真的疯了!
答案 0 :(得分:1)
我找到了解决方案,以及真正的问题。我在 Eclipse 中编辑 server.xml ,但我没有看到真正的发布结果。出于奇怪的原因, eclipse 删除了真正的 server.xml 中的资源链接,但是在 eclipse 视图中显示了该链接。所以我整天都在gooogled,我发现我必须在 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>
答案 1 :(得分:0)
您是否在web.xml中找到了资源的链接:
<resource-ref>
<description>JTDS Datasource</description>
<res-ref-name>jdbc/main</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
了解更多信息,请查看Tomcat文档:http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
答案 2 :(得分:0)
通常,JDBC数据源也配置为<Resource>
,如下所示
<Resource name="jdbc/main" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="******" password="######" driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://10.4.1.85/SBAGRU_DEV"/>
您可以尝试使用此内部标记。只需将上面<ResourceLink>
元素替换为<Resource>
。
您所做的工作似乎是正确的,因为<ResourceLink>
用于链接<GlobalNamingResources>
中定义的资源,不知道为什么它不起作用。
答案 3 :(得分:0)
请看一下这个链接 Hibernate + Tomcat 7
我遇到了同样的问题:在Tomcat更新之后,这个bug已经消失了。