【问题标题】:How do I switch between different configurations in GWT?如何在 GWT 中的不同配置之间切换?
【发布时间】:2014-04-02 00:11:01
【问题描述】:

我有一个 GWT 项目在开发和生产模式以及网络和移动设备上运行。

每种模式都有不同的 web.xml 文件。

每个版本我还需要不同的常量。目前我使用这个:

class Params {

   public static final String SOME_CONSTANT = "value";
   ...
}

SOME_CONSTANT 的值可能会随着模式(应用程序的版本)而变化。

如何为每种模式(开发、产品、网络、移动)设置不同的常量?

【问题讨论】:

    标签: java javascript html gwt


    【解决方案1】:

    将这些常量移动到每个环境的属性文件中。

    创建一个这样的文件夹(它必须在您最终生成的战争文件之外,在服务器上的某个位置)

    resources
     |__dev
     |__prod
     |__web
     |__mobile
    

    每个文件夹都包含具有基于环境的值的属性文件。

    在服务器启动时将 environment 的值作为系统属性或环境变量传递。在应用程序上下文初始化时加载所有属性并在应用程序的任何位置使用它。

    使用 ServletContextListener 在服务器启动时读取所有属性。

    如何根据系统属性或环境变量加载属性文件?

    使用

     System.getProperty()
    

    System.getenv()
    

    读取属性文件的位置。

    并加载属性文件

    Properties properties = new Properties()
    properties.load(new FileInputStream(new File(absolutePath)));
    

    您可以将属性存储为可以从包括 JSP 在内的任何地方读取的应用程序上下文属性。


    --编辑--

    在服务器启动时加载属性文件:

    web.xml

    <listener>
        <listener-class>com.x.y.z.server.AppServletContextListener</listener-class>
    </listener>
    

    AppServletContextListener.java

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class AppServletContextListener implements ServletContextListener {
    
        private static Properties properties = new Properties();
    
        static {
            // load properties file
            String absolutePath = null;
            if (System.getenv("properties_absolute_path") == null) {
                absolutePath = System.getProperty("properties_absolute_path");
            } else {
                absolutePath = System.getenv("properties_absolute_path");
            }
            try {
                File file = new File(absolutePath);
                properties.load(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            servletContextEvent.getServletContext().setAttribute("properties", properties);
        }
    
        public static Properties getProperties() {
            return properties;
        }
    
    }
    

    【讨论】:

    • 这是在 GWT 客户端吗?能否举个更详细的例子?
    • resources 文件夹必须在您最终生成的战争之外。将资源文件夹放在服务器上的某个位置。在服务器启动时使用 servlet 上下文侦听器加载属性文件。使用从入口点类调用的简单 RPC 调用在客户端传递加载的属性文件。你可以再做一件事。将属性存储为应用程序范围的属性,可以从任何地方读取,包括 JSP。
    • 能否更新您的描述并提供更详细的示例?如果PhoneGap环境没有服务器,你会怎么做?
    • 你的意思是什么:“你可以再做一件事。将属性存储为应用程序范围的属性,可以从任何地方读取,包括 JSP”你能举个例子吗?
    • 你知道网络应用程序中的application scope吗?我已经更新了我的帖子,让你更清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多