当前位置: 首页 > 面试题库 >

GWT与JDO问题

夏侯弘量
2023-03-14
问题内容

我只是开始玩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. 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>

我试图在这些教程中添加Key类以及其他所有内容,但似乎我缺少了一些东西。

这是我的错误:
替代文字http://vasura.s3.amazonaws.com/Picture2.png

或在我收到此错误之前:

密钥无法解析为类型

使此工作最佳的解决方案是什么?


问题答案:

Sriram Narayan说要对密钥进行字符串编码,以使其通过GWT的RPC机制传递:

@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;



 类似资料:
  • 13.3. JDO Spring支持标准的JDO 1.0/2.0 API作为数据访问策略,同样遵循类似于Spring对Hibernate的支持风格。对应的支持与整合类位于 org.springframework.orm.jdo 包中。 13.3.1. 建立PersistenceManagerFactory Spring提供了一个 LocalPersistenceManagerFactoryBean

  • Java DataObjects(JDO)提供java数据对象规范,支持所有的数据存储(RDBMS, ODBMS, XML, LDAP等)。JDO 2.2采用JDO2规范,提供: 1。支持独立事务层规范 2。能够动态获得组(通过API) 3。能够设置数据源为只读属性,避免任何写操作 4。支持选择哪一个classes/fields进入L2缓存 Apache JDO 是 JDO 规范的一个实现。

  • 亲爱的读者,这些GWT Interview Questions专门设计用于让您熟悉在面试GWT期间可能遇到的问题的性质。 根据我的经验,很好的面试官在你的面试中几乎不打算问任何特定的问题,通常问题从这个主题的一些基本概念开始,然后他们继续基于进一步的讨论和你回答的问题 - 什么是GWT? Google Web Toolkit(GWT)是一个用于构建和优化基于浏览器的复杂应用程序的开发工具包。 Go

  • 问题内容: 我想使用Struts2在Google App Engine上开发项目。对于数据库,我有两个选项JPA和JDO。你们能建议我吗?两者对我来说都是新手,我需要学习它们。因此,在您回复之后,我将重点介绍一个。 谢谢。 问题答案: JPA是Sun的持久性标准,JDO快死了(实际上,它已经死了,但仍在发展)。换句话说,从长远来看,JPA似乎是更好的投资。因此,如果两者都不是我的话,我想我会选择J

  • 我正在使用相对较新的用于Eclipse的Google Cloud Tools插件,取代用于Eclipse的Google插件。我已将我的应用程序引擎项目转换为新插件的环境,但现在应用程序停止工作。 在请求使用Google数据存储条目的servlet时,我遇到以下错误: GPE(谷歌插件Eclipse)对此有一些选择:https://developers.google.com/eclipse/docs

  • 我知道其他人在GWT编译方面有问题,但在这种情况下,我感到茫然。我使用的是GWT2.6.1和相同版本的gwt-maven-plugin2.6.1 首先,GWT和/或SmartGWT是否支持JDK1.8,或者目前只支持1.7?我使用的是JDK1.7,但最终还是想尝试一下1.8。 其次,当我在eclipse中进行maven构建时,我得到了这个错误...然而,当我从GWT eclipse插件进行GWT编