我有一个新编码的 GWT/GAE 应用程序,它在客户端使用 RequestFactory 和 Editors,在背面使用自定义 Objectify DAO 服务。
flush() 然后 persist() 路径在成功时可以正常工作。 客户端 JSR 303 也可以正常工作。
我的问题是如何触发服务器警告/错误并处理 UI 更新?
我在 Objectify 2 中使用 Chandler 的通用 DAO http://turbomanage.wordpress.com/2010/02/09/generic-dao-for-objectify-2/
我的 gwt Activity 正在调用 persist(myProxy).fire(new Receiver<>)
我的 dao 代码抛出 IllegalArgumentException 和其他 RuntimeExceptions 用于业务逻辑情况,例如“发现重复的电子邮件地址 - 想要登录?”
Receiver<>.onSuccess() 可以很好地跟踪成功的结果。 Receiver<>.onFailure() 和 Receiver<>.onViolation() 都不会报告 RuntimeExceptions。
(更正:针对服务器端异常调用 onFailure())
有没有更好的方法来做到这一点? DAO 应该抛出哪些异常以使 onViolation() 或 onFailure() 报告错误? 编辑器应如何处理异常并从异常中恢复?
最佳答案
我发现最通用的命令序列是
void start() {
// Either get p
context1.get(..).to( new Receiver<P> { onSuccess(P resp){p = resp;} ... }).fire();
// OR create p
p = context2.create( P.class );
// Then save p
req = context2.persist(p).to( new Receiver<P>{ /* note do not use context1 */
onViolation(...) { /*JSR 303 handler*/ };
onFailure( error ) { /* handle */ error.getMessage() };
onSuccess(X x) { /* whatever persist() returns handler */ }; } );
// drive editor with p
driver.edit( p, req);
}
....
void onSave() {
// editor
ctxt = driver.flush() /* note ctxt == context2 */
if ( driver.hasErrors() ) { /*JSR 303 handler*/};
// RF
ctxt.fire();
}
Thomas Broyer onFailure should containg the getMessage() of the exception you threw on the server side.
You can tweak it by providing your own ExceptionHandler to the RequestFactoryServlet (extend it and use its constructor taking an ExceptionHandler).
onViolation will only be called if your entities do not pass JSR-303 Bean Validation, which is checked before calling any service method.
If you want to "catch" the failure in clidnt code, you have to add a Receiver for the persist() service method:
context.persist(p).to(new Receiver…
关于java - 处理 GWT RequestFactory 服务器错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5277084/