Java 如何在GWT中的不同配置之间切换?
我有一个GWT项目运行在开发和生产模式以及web和移动设备上 对于每种模式,我都有不同的web.xml文件 我还需要每个版本的不同常数。目前我使用的是:Java 如何在GWT中的不同配置之间切换?,java,javascript,html,gwt,Java,Javascript,Html,Gwt,我有一个GWT项目运行在开发和生产模式以及web和移动设备上 对于每种模式,我都有不同的web.xml文件 我还需要每个版本的不同常数。目前我使用的是: class Params { public static final String SOME_CONSTANT = "value"; ... } 某些_常量的值可能会在不同模式(应用程序版本)之间发生变化 如何为每种模式(开发、产品、网络、移动)设置不同的常数? 将这些常量移动到每个环境的属性文件中 创建这样一个文件夹(它必须位
class Params {
public static final String SOME_CONSTANT = "value";
...
}
某些_常量的值可能会在不同模式(应用程序版本)之间发生变化
如何为每种模式(开发、产品、网络、移动)设置不同的常数?
将这些常量移动到每个环境的属性文件中
创建这样一个文件夹(它必须位于最终生成的war文件之外,服务器上的某个位置)
每个文件夹都包含具有基于环境的值的属性文件
在服务器启动时将环境值作为系统属性或环境变量传递。在应用程序上下文初始化时加载所有属性,并在应用程序中的任何位置使用它
使用ServletContextListener在服务器启动时读取所有属性
如何基于系统属性或环境变量加载属性文件
使用
或
读取属性文件的位置
并加载属性文件
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>
这是在GWT客户端吗?请给出一个更详细的例子好吗?资源文件夹必须在最终生成的war之外。将资源文件夹放在服务器上的某个位置。使用servlet上下文侦听器在服务器启动时加载属性文件。使用从入口点类调用的简单RPC调用在客户端传递加载的属性文件。你可以再做一件事。将属性存储为应用程序范围的属性,该属性也可以从任何地方(包括JSP)读取。请更新您的描述并提供更详细的示例?如果PhoneGap环境中没有服务器,您会怎么做?您这样说是什么意思:“您还可以做一件事。将属性存储为应用程序范围的属性,可以从任何地方(包括JSP)读取”您能举个例子吗?您知道web应用程序中的
应用程序范围?我已经更新了我的帖子,让你更清楚。
System.getenv()
Properties properties = new Properties()
properties.load(new FileInputStream(new File(absolutePath)));
<listener>
<listener-class>com.x.y.z.server.AppServletContextListener</listener-class>
</listener>
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;
}
}