gpt4 book ai didi

java - 带有 DropDownChoice 的 Wicket 表单,带有枚举和CompoundPropertyModel

转载 作者:行者123 更新时间:2023-12-01 13:30:27 25 4
gpt4 key购买 nike

我在使用具有如下下拉选项的 Java Wicket 表单时遇到问题:

public class SomePage extends WebPage { 
public SomePage () {
CompoundPropertyModel<SomeBean> properties = new CompoundPropertyModel<SomeBean>(new SomeBean());
Form<SomeBean> form = new Form<SomeBean>("SomeBeanForm", properties) {
//override on submit
};
form.add(new DropDownChoice<MyEnum>("myEnum", Lists.newArrayList(MyEnum.values()));
add(form);
}
}

SomeBean 很简单,如下所示:

public class SomeBean {
private MyEnum myEnum;
// Getter, Setter
public SomeBean(){
}
}

MyEnum 是:

public enum MyEnum { 
CHOICE1("1"),
CHOICE2("2");
private String id;
public MyEnum(String id) {
this.id=id;
}
//some methods to get/set/display id
}

我的问题是如何使用CompoundPropertyModel 在bean 中设置枚举?我收到一个异常,例如:

Could not convert value: CHOICE1 to type: MyEnum Could not find compatible converter.

这是由于它不知道如何将任何内容转换为 MyEnum 对象,它需要一个易于创建的转换器,但是如何为由CompoundPropertyModel创建的PropertyModel设置转换器?

感谢您的帮助!

--编辑--

这是一个错误的枚举导入,bean 和下拉选项使用了具有相同名称的不同枚举。这段代码有效。

最佳答案

您的代码不包含实体名称作为组件ID,这就是使用CompoundPropertyModel的原因。

查看 https://repo.twinstone.org/projects/WISTF/repos/wicket-examples-6.x/browse 上的完整代码示例

HTML 片段:

<form wicket:id="form">
<label>Text</label>: <input type="text" wicket:id="text" /> <br/>
<select wicket:id="choice"></select>

<input wicket:id="submit" type="submit" value="Send" />
</form>

代码片段:

final IChoiceRenderer<ChoiceEnum> CHOICE_RENDERER = new ChoiceRenderer<ChoiceEnum>("description");

inal List<ChoiceEnum> ALL_CHOICES = Arrays.asList(ChoiceEnum.values());

final IModel<TextAndEnumBean> model = Model.of(new TextAndEnumBean());
final IModel<TextAndEnumBean> compoundModel = CompoundPropertyModel.of(model);

Form<TextAndEnumBean> form = new Form<>("form", compoundModel);
add(form);

form.add(new TextField<String>("text"));

form.add(new DropDownChoice<ChoiceEnum>("choice", ALL_CHOICES, CHOICE_RENDERER).setNullValid(true));

form.add(new Button("submit"));

实体Bean代码:

public class TextAndEnumBean implements Serializable {

private static final long serialVersionUID = 1L;

private String text;

private ChoiceEnum choice;

public TextAndEnumBean() {
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public ChoiceEnum getChoice() {
return choice;
}

public void setChoice(ChoiceEnum choice) {
this.choice = choice;
}

}

枚举定义代码:

public enum ChoiceEnum {

CHOICE1("choice no. 1"),
CHOICE2("choice no. 2"),
MY_CHOICE("example of my choice"),
ANTOHER_CHOICE("another choice");

private final String description;


private ChoiceEnum(String description) {
this.description = description;
}


public String getDescription() {
return description;
}


}

关于java - 带有 DropDownChoice 的 Wicket 表单,带有枚举和CompoundPropertyModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21599478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com