[英]GWT - impossible to find working dir with Eclipse
我需要在面板上显示工作目录。
我使用String value = System.getProperty("user.dir")
。 之后,我将此字符串放在标签上,但在控制台上收到此消息:
The method getProperty(String, String) in the type System is not applicable for the arguments (String).
我用日食。
我猜您尚未通过GWT 101-您不能在客户端盲目使用JAVA CODE。
您可以从JAVA中找到GWT支持的类和方法的列表。 https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation
对于系统,仅支持以下内容。
err, out,
System(),
arraycopy(Object, int, Object, int, int),
currentTimeMillis(),
gc(),
identityHashCode(Object),
setErr(PrintStream),
setOut(PrintStream)
在您的情况下,请在服务器端代码中执行System.getProperty(“ user.dir”)并使用RPC或任何其他服务器端gwt通信技术对其进行访问。
不支持System.getProperty(“ key”),但支持System.getProperty(“ key”,“ default”),尽管它仅会返回默认值,因为本身没有系统属性。
如果在gwt编译期间需要工作目录,则需要使用自定义链接器或生成器,在构建时获取系统属性,并将其作为公共资源文件发出。
对于链接器,您必须导出gwt可以下载并获取所需编译时数据的外部文件。 对于生成器,您只需将所需的字符串注入已编译的源即可。
这是一个非常有趣的链接器幻灯片。
http://dl.google.com/googleio/2010/gwt-gwt-linkers.pdf
如果您不想使用链接器和额外的http请求,则也可以使用生成器,这可能会更容易(并且更快):
interface BuildData {
String workingDirectory();
}
BuildData data = GWT.create(BuildData.class);
data.workingDirectory();
然后,您需要制作一个生成器:
public class BuildDataGenerator extends IncrementalGenerator {
@Override
public RebindResult generateIncrementally(TreeLogger logger,
GeneratorContext context, String typeName){
//generator boilerplate
PrintWriter printWriter = context.tryCreate(logger, "com.foo", "BuildDataImpl");
if (printWriter == null){
logger.log(Type.TRACE, "Already generated");
return new RebindResult(RebindMode.USE_PARTIAL_CACHED,"com.foo.BuildDataImpl");
}
SourceFileComposerFactory composer =
new SourceFileComposerFactory("com.foo", "BuildDataImpl");
//must implement interface we are generating to avoid class cast exception
composer.addImplementedInterface("com.foo.BuildData");
SourceWriter sw = composer.createSourceWriter(printWriter);
//write the generated class; the class definition is done for you
sw.println("public String workingDirectory(){");
sw.println("return \""+System.getProperty("user.dir")+"\";");
sw.println("}");
return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING
,"com.foo.BuildDataImpl");
}
}
最后,您需要告诉gwt在界面上使用生成器:
<generate-with class="dev.com.foo.BuildDataGenerator">
<when-type-assignable class="com.foo.BuildData" />
</generate-with>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.