开发者

GWT: Dealing with null request factory proxy references in view

I have a view which displays user account details. Corresponding activity asks server-side code (in order to retrieve user account object) over GWT request factory. View interface extends TakesValue< T > interface, so my ViewImpl< UserAccountProxy > implements setValue(UserAccountProxy) method. As intended, this method is used to set reference to the object which will be displayed currently.

UserAccountProxy has many properties, thus its proxy refers to many other proxy objects. So, for example we can have: UserNameProxy, UserPasswordProxy, UserRolesProxy and so on encapsulated in UserAccountProxy. Almost every of these proxies may be null in some circumstances (user has no roles specified, user has no avatar, no signature specified...). Also, it is possible that corresponding activity wouldn't ask request factory for some part of properties, and then, also, these properties would be null.

If any of these properties (proxy) is not null, then it should be displayed in view by using appropriate widget.

The simplest, and the most ugly approach to avoid NullPointerException is following:

public void setValue(UserAccountProxy account) {
   //getUserName() returns proxy which may be null
   if (account.getUserName() != null) { 
     setUserNameWi开发者_StackOverflowdget(account.getUserName());
   }
   //getUserPassword() returns proxy which may be null
   if (account.getUserPassword() != null) {
     setUserPasswordWidget(account.getUserPassword());
   }
   // (....)
}

Of course, I'd like to avoid this type of coding, but I don't have a clue how to do it in the other way.

Thanks in advance for any hints.


The GWT Editor framework can reduce the amount of glue code that you need to write to bind the data model and the UI together. There's a RequestFactoryEditorDriver type that adds RequestFactory-specific features to the Editor framework. The use of an OptionalFieldEditor adapter will allow the nullable properties to control whether or not the corresponding widget is created or discarded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜