GWT与JDO问题 [英] GWT with JDO problem
问题描述
我刚开始玩GWT我很难让GWT + JAVA + JDO + Google AppEngine与DataStore一起工作。
我试图按照不同的教程,但没有运气。例如,我参考了这些教程: TUT1 TUT2
我无法弄清楚为了完成这项工作,我需要做些什么和做什么。
请看我的简单代码并告诉我我需要做什么,因此我可以将其保存到数据存储区:
1。 ADDRESS ENTITY
package com.example.rpccalls.client;
import java.io.Serializable;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
public class Address implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private int addressID;
@Persistent private String address1;
@Persistent private String address2;
@Persistent私人字符串城市;
@Persistent私有字符串状态;
@Persistent私人字符串zip;
public Address(){}
$ b $ public address(String a1,String a2,String city,String state,String zip){
this.address1 = a1 ;
this.address2 = a2;
this.city = city;
this.state = state;
this.zip = zip;
$ b $ * Setters and Getters * /
}
。2。 PERSON实体
package com.example.rpccalls.client;
import java.io.Serializable;
import java.util.ArrayList;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
@PersistenceCapable
public class Person实现Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
私钥键;
@Persistent私人字符串名称;
@Persistent私人年龄;
@Persistent私人字符性别;
@Persistent ArrayList<地址>地址;
public Person(){}
$ b $ public Person(String name,int age,char gender){
this.name = name;
this.age =年龄;
this.gender = gender;
$ b $ * Getters and Setters * /
}
。3。 RPCCalls
package com.example.rpccalls.client;
import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
public class RPCCalls实现EntryPoint {
private static final String SERVER_ERROR =尝试联系服务器时发生错误,请检查您的网络连接并尝试再次。;
private final RPCCallsServiceAsync rpccallService = GWT.create(RPCCallsService.class);
TextBox nameTxt = new TextBox();
Button btnSave = getBtnSave();
$ b $ public void onModuleLoad(){
RootPanel.get(inputName).add(nameTxt);
RootPanel.get(btnSave).add(btnSave);
$ b私人按钮getBtnSave(){
按钮btnSave =新建按钮(SAVE);
btnSave.addClickHandler(
new ClickHandler(){
public void onClick(ClickEvent event){
saveData2DB(nameTxt.getText());
}
}
);
返回btnSave;
}
void saveData2DB(String name){
AsyncCallback< String> callback = new AsyncCallback< String>(){
public void onFailure(Throwable caught){
Window.alert(WOOOHOOO,ERROR:+ SERVER_ERROR);
// TODO:做一些有错误的事情。
}
public void onSuccess(String result){
Window.alert(Server is saying:'+ result +');
}
};
ArrayList< Address> aa = new ArrayList< Address>();
aa.add(新地址(123 sasdf,,某个城市,AZ,93923-2321));
aa.add(新地址(23432 asdf,Appt 34,另一个城市,AZ,43434-4432));
Person p = new Person();
p.setName(name);
p.setAge(23);
p.setGender('m');
p.setAddresses(aa);
// !!!!!!!!!!!!!!!!!!服务器电话!!!!!!!!!!!!!!!!!!
rpccallService.saveName(p,callback);
// !!!!!!!!!!!!!!!!!!服务器电话!!!!!!!!!!!!!!!!!!
$ b 4。 RPCCallsService
package com.example.rpccalls.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath(calls)
public interface RPCCallsService extends RemoteService {
String saveName(Person p);
}
5。 RPCCallsServiceAsync
package com.example.rpccalls.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface RPCCallsServiceAsync {
void saveName(Person p,AsyncCallback< String> callback);
}
6。 ** RPCCalls.gwt.xml
<?xml version =1.0encoding =UTF-8 >?;
<!DOCTYPE模块PUBLIC - // Google Inc. //DTD Google Web Toolkit 1.6.4 // ENhttp://google-web-toolkit.googlecode.com/svn/tags/1.6 0.4 /发行源/型芯/ SRC / GWT-module.dtd>
< module rename-to ='rpccalls'>
<继承了name ='com.google.gwt.user.User'/>
<继承了name ='com.google.gwt.user.theme.standard.Standard'/>
< entry-point class ='com.example.rpccalls.client.RPCCalls'/>
< / module>
我试图在这些教程中添加Key类和其他所有内容,但它看起来像是在丢失一些东西。
这是我的错误:
替代文字http://vasura.s3.amazonaws.com/Picture2.png
或之前我得到这个错误:
键无法解析为类型
是使这项工作最好的解决方案吗? /4/gwt-and-jdo-on-java-appengine.htmlrel =nofollow noreferrer> Sriram Narayan 说要对密钥进行字符串编码以使其通过GWT的RPC机制:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class SomeDomainClass implements Serializable {
@PrimaryKey
@Persistent(va lueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName =datanucleus,key =gae.encoded-pk,value =true)
String id; >
I just start playing with GWT I'm having a really hard time to make GWT + JAVA + JDO + Google AppEngine working with DataStore.
I was trying to follow different tutorial but had no luck. For example I wend to these tutorials: TUT1 TUT2
I was not able to figure out how and what i need to do in order to make this work.
Please look at my simple code and tell me what do i need to do so i can persist it to the datastore:
1. ADDRESS ENTITY
package com.example.rpccalls.client;
import java.io.Serializable;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
public class Address implements Serializable{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private int addressID;
@Persistent private String address1;
@Persistent private String address2;
@Persistent private String city;
@Persistent private String state;
@Persistent private String zip;
public Address(){}
public Address(String a1, String a2, String city, String state, String zip){
this.address1 = a1;
this.address2 = a2;
this.city = city;
this.state = state;
this.zip = zip;
}
/* Setters and Getters */
}
2. PERSON ENTITY
package com.example.rpccalls.client;
import java.io.Serializable;
import java.util.ArrayList;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
@PersistenceCapable
public class Person implements Serializable{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent private String name;
@Persistent private int age;
@Persistent private char gender;
@Persistent ArrayList<Address> addresses;
public Person(){}
public Person(String name, int age, char gender){
this.name = name;
this.age = age;
this.gender = gender;
}
/* Getters and Setters */
}
3. RPCCalls
package com.example.rpccalls.client;
import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
public class RPCCalls implements EntryPoint {
private static final String SERVER_ERROR = "An error occurred while attempting to contact the server. Please check your network connection and try again.";
private final RPCCallsServiceAsync rpccallService = GWT.create(RPCCallsService.class);
TextBox nameTxt = new TextBox();
Button btnSave = getBtnSave();
public void onModuleLoad() {
RootPanel.get("inputName").add(nameTxt);
RootPanel.get("btnSave").add(btnSave);
}
private Button getBtnSave(){
Button btnSave = new Button("SAVE");
btnSave.addClickHandler(
new ClickHandler(){
public void onClick(ClickEvent event){
saveData2DB(nameTxt.getText());
}
}
);
return btnSave;
}
void saveData2DB(String name){
AsyncCallback<String> callback = new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
Window.alert("WOOOHOOO, ERROR: " + SERVER_ERROR);
// TODO: Do something with errors.
}
public void onSuccess(String result) {
Window.alert("Server is saying: ' " + result + "'");
}
};
ArrayList<Address> aa = new ArrayList<Address>();
aa.add(new Address("123 sasdf","", "Some City", "AZ", "93923-2321"));
aa.add(new Address("23432 asdf", "Appt 34", "Another City", "AZ", "43434-4432"));
Person p = new Person();
p.setName(name);
p.setAge(23);
p.setGender('m');
p.setAddresses(aa);
// !!!!!!!!!!!!!!!!!! SERVER CALL !!!!!!!!!!!!!!!!!!
rpccallService.saveName(p, callback);
// !!!!!!!!!!!!!!!!!! SERVER CALL !!!!!!!!!!!!!!!!!!
}
}
4. RPCCallsService
package com.example.rpccalls.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("calls")
public interface RPCCallsService extends RemoteService {
String saveName(Person p);
}
5. RPCCallsServiceAsync
package com.example.rpccalls.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface RPCCallsServiceAsync {
void saveName(Person p, AsyncCallback<String> callback);
}
6. **RPCCalls.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6.4//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.6.4/distro-source/core/src/gwt-module.dtd">
<module rename-to='rpccalls'>
<inherits name='com.google.gwt.user.User'/>
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<entry-point class='com.example.rpccalls.client.RPCCalls'/>
</module>
I tried to add Key class and everything else in those tutorials but it looks like i'm missing something.
Here is my error:
alt text http://vasura.s3.amazonaws.com/Picture2.png
or before i was getting this error:
Key cannot be resolved to a type
What is the best solution to make this working?
解决方案 Sriram Narayan says to String-encode the Key to get it to pass through GWT's RPC mechanism:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class SomeDomainClass implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
String id;
这篇关于GWT与JDO问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!