简体   繁体   English

为什么在GWT开发模式下不能从服务器端访问在客户端创建的对象?

[英]Why an object, created in client side, is not accessible from server side in GWT development mode?

I am running my GWT application in development mode. 我正在开发模式下运行我的GWT应用程序。 In my client side code (Classes that implements Entrypoint), I have created one object and assigned it to a static variable of another class (Suppose Class A). 在我的客户端代码(实现Entrypoint的类)中,我创建了一个对象,并将其分配给另一个类的静态变量(假设类A)。

Now, expectedly I can access the object by he reference of the static variable of class A from anywhere from the client side but can't access it from the server side code. 现在,预期我可以通过他从客户端任何地方引用类A的静态变量来访问该对象,但不能从服务器端代码访问它。 Can anybody explain me why I can't access the object the same way I accessed it from client side code as both the client and server running within the same session scope on the same JVM in development mode. 谁能解释我为什么不能以与从客户端代码访问对象相同的方式访问对象,因为在开发模式下,客户端和服务器都在同一JVM上的同一会话范围内运行。

Your client side code and server are not running inside one JVM, even in a dev mode. 您的客户端代码和服务器即使在开发模式下也不会在一个JVM中运行。 As you know, GWT client code is compiled into Javascript code. 如您所知,GWT客户端代码被编译为Javascript代码。 So updating some static field is eventually compiled into updating some property of Javascript object. 因此,更新某些静态字段最终被编译为更新Javascript对象的某些属性。 In can not be accessed by Server side code for obvious reasons. 由于明显的原因,无法通过服务器端代码访问。

Dev mode in GWT is smart enough and not allows to mix Server and Client static variables to avoid confusion after deploying compiled version to your server. GWT中的Dev模式足够聪明,并且不允许混合使用Server和Client静态变量,以免在将编译版本部署到服务器后产生混淆。

You can not access client side classes directly from server side classes and vice versa. 您不能直接从服务器端类访问客户端类,反之亦然。

If you want to share some thing between client and server then use shared package but still its for specific purpose such as constants, validators, utilities, DTO (Data Transfer Object) etc. 如果要在客户端和服务器之间共享某些内容,请使用共享包,但仍将其用于特定目的,例如常量,验证器,实用程序,DTO(数据传输对象)等。

Do not use any GWT UI specific code in shared classes. 不要在共享类中使用任何GWT UI特定的代码。 Shared package is also visible to server and there is no meaning of your customized widgets. 共享软件包也对服务器可见,并且自定义窗口小部件没有意义。

Read here about GWT MVP atchitecture 在此处阅读有关GWT MVP功能的信息

Read more here about GWT Architectural Perspectives 在此处阅读更多有关GWT建筑的观点


Create your objects in shared package to communicate between client and server. 在共享程序包中创建对象以在客户端和服务器之间进行通信。

One more thing any changes made at client side will not reflect at server side whether it is stored in shared classes or not. 在客户端进行的任何更改都不会在服务器端反映出另一件事,无论该更改是否存储在共享类中。 You have to pass the value to the server via GWT RPC calls. 您必须通过GWT RPC调用将值传递给服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM