使用GWT无法读取服务器端的属性文件 [英] Can't read property file on Server side with GWT

查看:52
本文介绍了使用GWT无法读取服务器端的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器端读取属性文件.我将DBConfig.java,useDBConfig.java和DBConfig.properties都放置在服务器程序包中.我无法从服务器端的属性文件中读取值.非常感谢您的帮助.

I want to read property file on Server side. I have DBConfig.java, useDBConfig.java and DBConfig.properties all placed in server package. I can't read the values from property file on Server Side. Your help is highly appreciated.

public interface DBConfig extends Constants {
@DefaultStringValue("host")
String host(String host);

@DefaultStringValue("port")
String port(String port);

@DefaultStringValue("username")
String username(String username);

@DefaultStringValue("password")
String password(String password);

}


public void useDBConfig() {
DBConfig constants = GWT.create(DBConfig.class);
Window.alert(constants.host());


host = constants.host(host);
port = constants.port(port);
username = constants.username(username);
password = constants.password(password);

}

属性文件...

host=127.0.0.1
port=3306
username=root
password=root

谢谢.

推荐答案

GWT.Create仅可在客户端模式下使用.您确定代码在服务器端执行吗?

GWT.Create can be used only in client mode. Are you sure that code execute in server side?

如果我在应用程序GWT中写代码.在服务器端创建,则会出现此错误:

If i write in my application GWT.Create in server side i get this error:

java.lang.UnsupportedOperationException:错误:GWT.create()仅在客户端代码中可用!例如,不能从服务器代码中调用它.如果您正在运行单元测试,请检查您的测试用例是否扩展了GWTTestCase,并且未从初始化程序或构造函数中调用GWT.create().

java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code! It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.

您可以使用Java读取属性文件.该文件类似于GWT中的Constants文件.属性文件示例:

You can read a properties files in java. The file is similar that Constants files in GWT. Example of Properties file:

<代码>键=值主机= 127.0.0.1端口= 80用户名=客人密码=客人

EOF

您可以阅读此文件,请参见下面的代码:

You can read this file, see the next code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

String fileToRead = "MY_PATH"+File.separator+"MY_FILE.properties";
Properties prop = new Properties();
try {
    File propertiesFile = new File(fileToRead);
    prop.load(new FileInputStream(propertiesFile));
    String host = prop.getProperty("host");
    String port = prop.getProperty("port");
    String username = prop.getProperty("username");
    String password = prop.getProperty("password");

    System.out.println(host);
    System.out.println(port);
    System.out.println(username);
    System.out.println(password);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

如果键不存在,则getProperty(String key)返回null.您可以使用prop.containsKey(String key);查看密钥是否存在.此函数返回一个布尔值(在其他情况下,如果存在,则为True).

If the key doesnt exists getProperty(String key) return null. You can use prop.containsKey(String key); to see if the key exists. This function return a boolean (True if exists False in other case).

问候

这篇关于使用GWT无法读取服务器端的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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