且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Web应用程序中hsqldb文件的相对路径不起作用?

更新时间:2023-02-09 23:27:09

似乎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.