`

GWT AppEngine 保存对象序列号.

    博客分类:
  • java
阅读更多

想这个Google的app 都是可以先自己放一个免费的程序到服务器上面.

就是一个高兴的事情.

 

前几天研究了下GWT.关于做界面还是非常容易的.

 

java的代码想怎么写就怎么写.非常的高效率.

 

现在研究下如何将数据保存.

 

参考官方的文档.是通过JDO实现的.我也没有想太多.直接照着官方的文档写了个demo.

 

1.定义一个JDO对象.

 

package com.test.demo.jdo;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class LoginUser {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String userName;

	@Persistent
	private String passwd;

 

get set 方法和构造函数省略.

这里要说下.ID如果是要自增的话.id一定要用Long.而不能用String.

 

否则会报:ID自增错误.

 

然后用GWT写个对话框.

 

final DialogBox dialogBox = new DialogBox(false, true);
		// false:是否自动隐藏.(单击DialogBox之外的部分.true:是否是模式对话框.
		dialogBox.setTitle("登录对话框");
		dialogBox.setText("登录对话框");
		Grid grid = new Grid(3, 3);

		grid.setWidget(0, 0, new HTML("登录名:"));

		final TextBox userName = new TextBox();
		userName.setValue("demo");
		grid.setWidget(0, 1, userName);

		grid.setWidget(1, 0, new HTML("密码:"));

		final PasswordTextBox userPasswd = new PasswordTextBox();
		userPasswd.setValue("demo");
		grid.setWidget(1, 1, userPasswd);

		final Button submit = new Button("新增");
		submit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// dialogBox.hide();
				// GWT.
				loginUserAction.saveLoginUser(userName.getValue(), userPasswd
						.getValue(), new AsyncCallback<Void>() {

					public void onSuccess(Void result) {
						// TODO Auto-generated method stub
						System.out.println("save !");
						Window.alert("save!");
					}

					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
			}
		});

		final Button cancle = new Button("长度");
		cancle.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				loginUserAction.findLoginUserSize(new AsyncCallback<Integer>() {

					public void onSuccess(Integer result) {
						// TODO Auto-generated method stub
						Window.alert(result + "");
					}

					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
			}
		});

		grid.setWidget(2, 0, submit);
		grid.setWidget(2, 1, cancle);

		dialogBox.add(grid);
		dialogBox.center();
		dialogBox.show();
		// 将面板添加到dialog里面
 

 

private final LoginUserActionAsync loginUserAction = GWT
			.create(LoginUserAction.class);

 

在GWT里面调用jdo 保存数据和查询数据记录数.

 

http://toeoso.appspot.com/Login.html

 

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private PMF() {}

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

 看了下官方写了一个PMF类.我也拷贝了下.

 

public void deleteLoginUserById(String id) {

	}

	public void saveLoginUser(String userName, String userPasswd) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			LoginUser loginUser = new LoginUser(userName, userPasswd);
			//loginUser.setId(Math.abs(new Random().nextInt())+"");
			pm.makePersistent(new LoginUser(userName, userPasswd));
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	public int findLoginUserSize() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			javax.jdo.Query query = pm.newQuery(LoginUser.class);
			List<LoginUser> results = (List<LoginUser>) query.execute("Smith");
			return results.size();
		}catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			pm.close();
		}
	}

 用了下PMF.每次都要申请一个.然后在关闭下.不知道这么写对不对.

 

拷贝了下日志:

启动的时候创建了数据库表.

 
2009-9-19 11:22:00 com.google.apphosting.utils.jetty.JettyLogger info
信息: jetty-6.1.x
2009-9-19 11:22:00 com.google.apphosting.utils.jetty.JettyLogger info
信息: Started SelectChannelConnector@0.0.0.0:8080
The server is running at http://localhost:8080/
2009-9-19 11:23:09 org.datanucleus.plugin.NonManagedPluginRegistry resolveConstraints
INFO: Bundle "org.datanucleus.jpa" has an optional dependency to "org.datanucleus.enhancer" but it cannot be resolved
2009-9-19 11:23:09 org.datanucleus.plugin.NonManagedPluginRegistry resolveConstraints
INFO: Bundle "org.datanucleus" has an optional dependency to "org.eclipse.equinox.registry" but it cannot be resolved
2009-9-19 11:23:09 org.datanucleus.plugin.NonManagedPluginRegistry resolveConstraints
INFO: Bundle "org.datanucleus" has an optional dependency to "org.eclipse.core.runtime" but it cannot be resolved
2009-9-19 11:23:09 org.datanucleus.PersistenceConfiguration setProperty
INFO: Property datanucleus.rdbms.sql.allowAllSQLStatements unknown - will be ignored
2009-9-19 11:23:09 org.datanucleus.PersistenceConfiguration setProperty
INFO: Property datanucleus.rdbms.stringDefaultLength unknown - will be ignored
2009-9-19 11:23:09 org.datanucleus.PersistenceConfiguration setProperty
INFO: Property datanucleus.appengine.autoCreateDatastoreTxns unknown - will be ignored
2009-9-19 11:23:09 org.datanucleus.ObjectManagerFactoryImpl logConfiguration
INFO: ================= Persistence Configuration ===============
2009-9-19 11:23:09 org.datanucleus.ObjectManagerFactoryImpl logConfiguration
INFO: DataNucleus Persistence Factory - Vendor: "DataNucleus"  Version: "1.1.4"
2009-9-19 11:23:09 org.datanucleus.ObjectManagerFactoryImpl logConfiguration
INFO: DataNucleus Persistence Factory initialised for datastore URL="appengine" driver="" userName=""
2009-9-19 11:23:09 org.datanucleus.ObjectManagerFactoryImpl logConfiguration
INFO: ===========================================================
2009-9-19 11:23:09 org.datanucleus.PersistenceConfiguration setProperty
INFO: Property datanucleus.query.cached unknown - will be ignored
2009-9-19 11:23:10 org.datanucleus.jdo.metadata.JDOMetaDataManager <init>
INFO: Registering listener for metadata initialisation
2009-9-19 11:23:10 org.datanucleus.jdo.metadata.JDOAnnotationReader processClassAnnotations
INFO: Class "com.test.demo.jdo.LoginUser" has been specified with JDO annotations so using those.
2009-9-19 11:23:10 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Performing appengine-specific metadata validation for com.test.demo.jdo.LoginUser
2009-9-19 11:23:10 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Finished performing appengine-specific metadata validation for com.test.demo.jdo.LoginUser
2009-9-19 11:23:10 org.datanucleus.store.StoreDataManager registerStoreData
INFO: Managing Persistence of Class : com.test.demo.jdo.LoginUser [Table : <class name="LoginUser"
       identity-type="application"
       objectid-class="javax.jdo.identity.LongIdentity"
       persistence-modifier="persistence-capable"
>
<inheritance strategy="new-table">
</inheritance>
<field name="id"
       persistence-modifier="persistent"
       primary-key="true"
       null-value="none"
       default-fetch-group="true"
       embedded="false"
       value-strategy="identity"
       unique="false">
</field>
<field name="passwd"
       persistence-modifier="persistent"
       null-value="none"
       default-fetch-group="true"
       embedded="true"
       unique="false">
</field>
<field name="userName"
       persistence-modifier="persistent"
       null-value="none"
       default-fetch-group="true"
       embedded="true"
       unique="false">
</field>
</class>
, InheritanceStrategy : new-table]
2009-9-19 11:23:10 org.datanucleus.store.StoreDataManager registerStoreData
INFO: Managing Persistence of Class : com.test.demo.jdo.LoginUser [Table : <class name="LoginUser"
       identity-type="application"
       objectid-class="javax.jdo.identity.LongIdentity"
       persistence-modifier="persistence-capable"
>
<inheritance strategy="new-table">
</inheritance>
<field name="id"
       persistence-modifier="persistent"
       primary-key="true"
       null-value="none"
       default-fetch-group="true"
       embedded="false"
       value-strategy="identity"
       unique="false">
</field>
<field name="passwd"
       persistence-modifier="persistent"
       null-value="none"
       default-fetch-group="true"
       embedded="true"
       unique="false">
</field>
<field name="userName"
       persistence-modifier="persistent"
       null-value="none"
       default-fetch-group="true"
       embedded="true"
       unique="false">
</field>
</class>
, InheritanceStrategy : new-table]
2009-9-19 11:23:11 com.google.appengine.api.datastore.dev.LocalDatastoreService load
INFO: Time to load datastore: 141 ms
2009-9-19 11:23:11 org.datanucleus.jdo.metadata.JDOMetaDataManager$MetaDataRegisterClassListener registerClass
INFO: Listener found initialisation for persistable class com.test.demo.jdo.LoginUser
2009-9-19 11:23:49 org.datanucleus.ObjectManagerImpl close
INFO: Outstanding nontx update being committed to datastore
save !
2009-9-19 11:24:11 com.google.appengine.api.datastore.dev.LocalDatastoreService$PersistDatastore persist
INFO: Time to persist datastore: 0 ms
2009-9-19 11:52:28 org.datanucleus.ObjectManagerImpl close
INFO: Outstanding nontx update being committed to datastore
save !
2009-9-19 11:52:31 com.google.appengine.api.datastore.dev.LocalDatastoreService$PersistDatastore persist
INFO: Time to persist datastore: 62 ms

 

 

我在运行的时候遇到了

 

点击按钮的时候怎么也不响应.报后台类错误.

 

2009-9-19 10:56:15 com.google.apphosting.utils.jetty.JettyLogger warn
警告: EXCEPTION
java.lang.ClassNotFoundException: com.demo.test.server.LoginUserActionImpl
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:142)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.mortbay.util.Loader.loadClass(Loader.java:91)
    at org.mortbay.util.Loader.loadClass(Loader.java:71)
    at org.mortbay.jetty.servlet.Holder.doStart(Holder.java:73)
    at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:233)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:40)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:612)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:139)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1218)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:500)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:40)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:117)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:40)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:117)
    at org.mortbay.jetty.Server.doStart(Server.java:217)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:40)
    at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:152)
    at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:116)
    at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:218)
    at com.google.appengine.tools.development.gwt.AppEngineLauncher.start(AppEngineLauncher.java:86)
    at com.google.gwt.dev.HostedMode.doStartUpServer(HostedMode.java:365)
    at com.google.gwt.dev.HostedModeBase.startUp(HostedModeBase.java:590)
    at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:397)
    at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)

 

 

其实就是配置没有写对.类下面的这个和web.xml里面的要一致.

 

@RemoteServiceRelativePath("LoginUserAction")
public interface LoginUserAction extends RemoteService {
    void saveLoginUser(String userName,String userPasswd);
    void deleteLoginUserById(String id);
    int findLoginUserSize();
}

 

 

  <servlet>
    <servlet-name>LoginUserAction </servlet-name>
    <servlet-class>com.test.demo.server.LoginUserActionImpl</servlet-class>
  </servlet>
 
 <servlet-mapping>
    <servlet-name>LoginUserAction </servlet-name>
    <url-pattern>/com.test.demo.Login/LoginUserAction </url-pattern>
  </servlet-mapping>

 

叫什么是无所谓了..而且在代码里面也不用.

 

只不过是GWT自己去找而已.

 

想着也挺方便的.

 

 



 附件是代码.新增是将数据保存.长度是获得有几条数据.

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics