问题
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