从数据库访问和使用.jsf文件 [英] Accessing and using .jsf files from the database

查看:139
本文介绍了从数据库访问和使用.jsf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使我的Web应用程序能够使用数据库中存储的JSF文件的最佳方法是什么? 我希望能够动态地(在运行时)创建新的JSF页面,这些页面无需重新部署应用程序即可使用.

What is the best way to enable my webapplication to use JSF files stored in the database? I'd like to be able to dynamically (during runtime) create new JSF pages which will be made available without having to redeploy the application.

换句话说:我想将我的JSF页面的大部分存储在数据库中,并希望JSF将数据库用作获取JSF文件的数据源.

So in other words: I would like to store the bigger part of my JSF pages in the database and would like JSF to use the database as a datasource for getting JSF files.

我考虑了一个解决方案很长时间,并找到了一些可能的方法.但是,我都无法实现它们.

I've thought long about a solution and found some possible ways. However, I haven't been able to implement either of them.

  1. 每当需要添加/删除新页面时:操纵类路径中的文件(例如,将文件删除或添加到.war文件中)
  2. 扩展Web应用程序的类路径,以便它能够从运行时定义的位置(即/tmp或直接使用数据库连接)获取文件
  3. 为JSF提供一种以另一种方式查找资源的方法(这似乎不可能吗?)

我的环境:

  • Java SE 6
  • Jetty作为servlet容器
  • Mojarra作为jsf实现

现在,我的问题:

有人可以让JSF在默认类路径之外的位置(最好是数据库)查找页面吗?

任何回应,我们将不胜感激!

Any response is greatly appreciated!

推荐答案

1:每当需要添加/删除新页面时:处理类路径中的文件(例如,将文件删除或添加到.war文件中)

如果扩展了WAR,这绝对是可能的.我不确定Jetty,但它是否适用于Tomcat 7和Glassfish 3上的Mojarra2.x.只需使用通常的Java IO方式将文件写入扩展的WAR文件夹即可.

This is definitely possible if the WAR is expanded. I am not sure about Jetty, but it works for me with Mojarra 2.x on Tomcat 7 and Glassfish 3. Just writing the file to the expanded WAR folder the usual Java IO way suffices.

File file = new File(servletContext.getRealPath("/foo.xhtml"));

if (!file.exists()) {
    OutputStream output = new FileOutputStream(file);

    try {
        output.write(bytes); // Can be bytes from DB.
    } finally {
        output.close();
    }
}

此操作必须在FacesServlet插入之前执行.是一个理想的位置.另请参阅以下相关答案:

This needs to be executed before the FacesServlet kicks in. A Filter is a perfect place. See also this related answer:

2:扩展Web应用程序的类路径,以便它能够从运行时定义的位置(即/tmp或直接使用数据库连接)获取文件

您可以将Facelets文件打包到JAR文件中,并将其放在类路径中,并提供Facelets

You can package Facelets files in a JAR file and put it in the classpath and provide a Facelets ResourceResolver which serves the files from the JAR on when no match is found in WAR. You can find complete code examples in the following answers:

  • how to share a jsf error page between multiple wars
  • How to create a modular JSF 2.0 application?

3:为JSF提供了一种以另一种方式查找资源的方法(这似乎不可能吗?)

自定义ResourceResolver中有足够的游戏空间.

You've plenty of play room in the custom ResourceResolver.

这篇关于从数据库访问和使用.jsf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆