javax.naming.NamingException,Tomcat7和Struts2无法通过JNDI访问数据库资源 [英] javax.naming.NamingException, Tomcat7 and Struts2 can't access to DB resource via JNDI
问题描述
我正在使用Struts2框架(eclipse)开发新的Web应用程序,并且已经使用Hibernate映射了db结构.问题是,当我执行列表"操作时,框架会向我返回此错误:
I'm developing new web app with Struts2 framework, eclipse, and I've mapped the db structure with Hibernate. The problem is that when I execute my "list" action the framework returns me this error:
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是:
The server.xml is:
<?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
I've also 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>
有什么建议可以看的吗?我真的快要疯了!
Any suggestion where I can look? I'm really going crazy!
推荐答案
我找到了解决方案和实际问题.我在 Eclipse 中编辑 server.xml ,但没有看到真正的发布结果.出于奇怪的原因, eclipse 删除了真正的 server.xml 中的资源链接,但在 eclipse 视图中向我展示了该链接.因此,我整日忙于搜索,发现必须在 WEB-INF 目录下的 context.xml 文件中添加以下内容:
I found the solution, and the real problem. I edit the server.xml in Eclipse, and I didn't watch the real publish result. For strange reason eclipse remove the resource link in the real server.xml, but show me the link in eclipse view. So i gooogled all day long and I find that I must add in the context.xml file under WEB-INF directory this:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="">
<ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" />
</Context>
这篇关于javax.naming.NamingException,Tomcat7和Struts2无法通过JNDI访问数据库资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!