gpt4 book ai didi

java - GWT 与 JDO 问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:43:14 25 4
gpt4 key购买 nike

我刚开始玩 GWT 我真的很难让 GWT + JAVA + JDO + Google AppEngine 与 DataStore 一起工作。我试图遵循不同的教程,但没有运气。例如,我转到这些教程:TUT1 TUT2

我无法弄清楚如何以及需要做什么才能完成这项工作。请查看我的简单代码并告诉我我需要做什么才能将其保存到数据存储区:

<强>1。地址实体

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。个人实体

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。 RPC调用

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>

我尝试在这些教程中添加 Key 类和其他所有内容,但看起来我遗漏了什么。

这是我的错误: alt text http://vasura.s3.amazonaws.com/Picture2.png

或者在我收到这个错误之前:

Key cannot be resolved to a type

实现此功能的最佳解决方案是什么?

最佳答案

Sriram Narayan说要对 key 进行字符串编码以使其通过 GWT 的 RPC 机制:


@PersistenceCapable(identityType = IdentityType.APPLICATION)<br/>
public class SomeDomainClass implements Serializable {<br/>
@PrimaryKey<br/>
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)<br/>
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")<br/>
String id;<br/>

关于java - GWT 与 JDO 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/988217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com