GWT RPC serialization for Dynamic Host Page
I implemented a Dynamic Host Page (http://www.gwtproject.org/articles/dynamic_host_page.html) in my GWT project to pass my User POJO object directly in it.
I did it with AutoBean (https://code.google.com/p/google-web-toolkit/wiki/AutoBean) so I had to declare and implement Interfaces
But I found this way a little bit too verbose.
Is there another way? I mean GWT RPC do serialization without Interfaces so can I use it?
Another point; I already use the gwt-storage lib (https://github.com/seanchenxi/gwt-storage) that uses the GWT RPC mechanisim to store Java POJO Object in the Browser so there is probably an easy way...
THX
You could try to reuse GWT's com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter to serialize your POJO objects into string and put it into your dynamic host page.
Then you can use com.seanchenxi.gwt.storage.client.serializer.StorageSerializer to do the deserialization.
To get an instance of StorageSerializer, you should use GWT.create(StorageSerializer.class);
So thx you @xi-chen and @richard-wallis
Part of the solution were discussed on gwt-storage github repo (https://github.com/seanchenxi/gwt-storage/issues/8#issuecomment-68443910)
So it's totally possible with GWT RPC mechanism + gwt-storage :
index.jsp
<%@page import="com.google.gson.Gson"%>
<%@page import="com.learnkeeper.server.ServiceImpl"%>
<%@page import="com.google.gwt.user.server.rpc.RPC"%>
<%@page import="com.learnkeeper.shared.entities.User"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<%
Object user = request.getSession().getAttribute("user");
String serialized_user = new Gson().toJson(RPC.encodeResponseForSuccess(ServiceImpl.class.getMethod("getUser"), user).substring("//OK".length()));
%>
<script type="text/javascript">var serialized_user=<%=serialized_user%>;</script>
</head>
<body>
Notice you have to remove the //OK substring at the beginning of RPC.encodeResponseForSuccess result.
User.java
public class User implements IsSerializable {
}
Notice you have to implement IsSerializable ( Serializable is not enough)
HostPageUtils.java
public class HostPageUtils {
private static final native String getSerializedUser() /*-{
return $wnd.serialized_user;
}-*/;
public static User getUser() {
final String serializedUser = getSerializedUser();
StorageSerializer storageSerializer = GWT.create(StorageSerializer.class);
try {
return storageSerializer.deserialize(User.class, serializedUser);
} catch (Exception e) {
throw new RuntimeException();
}
}
}
链接地址: http://www.djcxy.com/p/51078.html
上一篇: GWT序列化策略文件
下一篇: 动态主机页面的GWT RPC序列化