GWT UiConstructor with optional parameters

十年热恋 提交于 2019-12-08 07:52:54

问题


We have a custom widget with 4 parameters. One of these parameters is optional. If we use the widget in UiBinder and omit the optional field an error is thrown.

For example

MyWidget(String arg1, String arg2, String arg3) {
  ...
}

If in the UiBinder file I write

...
<b:MyWidget arg1="sdfsd" arg2="fsdgds" arg3="ertlkj">
...

If I write

...
<b:MyWidget arg1="sdfsd" arg2="fsdgds">
...

Then the following error occurs:

[ERROR] <MyWidget arg1="sdfsd" arg2="fsdgds"> missing required attribute(s): arg3:     <b:MyWidget arg1="sdfsd" arg2="fsdgds">

Is there any way to define optional arguments?


回答1:


For better or worse, no - this is how UiBinder is designed. You can only designate one @UiConstructor, and UiBinder will always use that constructor.

If possible, instead of making them constructor arguments, make them setters. The setArg3() method will be optionally called if the arg3 attribute is present, rather than trying to switch between constructors.




回答2:


I don't see any optional parameters in your widget - all of them are required. You can do:

MyWidget(String arg1, String arg2) {
  ...
}

public void setArg3(String arg3) {
    this.arg3 = arg3;
}

Or:

MyWidget() {
}
public void setArg1(String arg1) {
    this.arg1 = arg1;
}
public void setArg2(String arg2) {
    this.arg2 = arg2;
}
public void setArg3(String arg3) {
    this.arg3 = arg3;
}

Then both of your Ui:Binder declarations should work.



来源:https://stackoverflow.com/questions/24209396/gwt-uiconstructor-with-optional-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!