Web应用程序中hsqldb文件的相对路径不起作用? [英] Relative path to hsqldb files in a web app doesn't work?
问题描述
我正在使用hsqldb作为基于Spring的java webapp。我将数据库文件(mydb.lck,mydb.properties,..)放在src\main\java \ data文件夹中,以便将它们发布到WEB-INF\classes \ data。
I'm using hsqldb for my Spring-based java webapp. I put database files (mydb.lck, mydb.properties,..) in src\main\java\data folder so that they're published into WEB-INF\classes\data.
在数据源配置中,我指定了JVM工作目录的相对路径。按照hsqldb文档中的指导。
In datasource configuration, I specify this relative path to JVM working directory. As guided in hsqldb documents.
portal.jdbc.url = jdbc:hsqldb:file:/ data / mydb
(这个分离器是否适用于Windows?)
portal.jdbc.url=jdbc:hsqldb:file:/data/mydb
(Is this seperator right for Windows?)
但是Spring似乎没有找到这条路并坚持要求
But Spring seem not find this path and insist on claiming
java.sql.SQLSyntaxErrorException:user缺少未找到的权限或对象:CUSTOMER
org.hsqldb.jdbc.Util.sqlException(Unknown Source)
但是,如果我指定一个绝对路径,它可以完美地工作
However, if I specify an absolute path, it works flawlessly
portal.jdbc .URL = JDBC:HSQLDB:文件:d:\\TomcatServer\\apache-Tomcat的7.0.10\\wtpwebapps\\myportal-app\\data\\mydb
我是否会想念Web应用程序上的JVM工作目录?
感谢任何帮助。
Should I miss understanding JVM working directory on a web app? Any help is appreciated.
推荐答案
似乎Tomcat没有为我们提供属性变量(如webroot) )引用我的应用程序上下文。所以我的解决方案是在Servlet上下文监听器中注册这样的属性。
It seems that Tomcat doesn't provide us a properties variable (like "webroot") to refer to my application context. So my solutions is to register such a properties in Servlet context listener.
我的代码:
public class WebAppPropertiesListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
String rootPath = sce.getServletContext().getRealPath("/");
System.setProperty("webroot", rootPath);
}
...
}
并在触发Spring上下文之前在web.xml中添加侦听器
And add listener in web.xml before Spring context is triggered
<listener>
<listener-class>com.iportal.util.WebAppPropertiesListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
然后我将该属性放在Hsqldb设置中。
Then I put the property in Hsqldb setting.
portal.jdbc.url=jdbc:hsqldb:file:${webroot}WEB-INF/classes/data/mydb
希望这对可能遇到同样问题的人有帮助。
Hope this helpful for someone who may run into the same problem.
这篇关于Web应用程序中hsqldb文件的相对路径不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!