PropertyModel 表达式的异常:org.apache.wicket.WicketRuntimeException:没有为类定义 get 方法:

发布于 2024-12-18 03:17:45 字数 1216 浏览 0 评论 0原文

我使用 PropertyModel 作为我的 DropDownChoice 的一部分,如下所示:

    List<String> choices = Arrays.asList(new String[] { "Library", "School Office", "Science Dept" });
    String selected = "Library";

    DropDownChoice<String> serviceDDC = 
            new DropDownChoice<String>("service",  new PropertyModel(this, "choices.0"), choices); 

不知何故,我遇到了这个异常:

caused by: org.apache.wicket.WicketRuntimeException: No get method defined for class: class com.samoo.tool.pages.CreatePrintingJob expression: choices
    at org.apache.wicket.util.lang.PropertyResolver.getGetAndSetter(PropertyResolver.java:481)
    at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:332)
    at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:242)
    at org.apache.wicket.util.lang.PropertyResolver.getValue(PropertyResolver.java:95)
    at org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:130)
    at org.apache.wicket.Component.getDefaultModelObject(Component.java:1724)

....

我知道表达式有问题。我一直在尝试不同的参数输入,但它仍然不起作用。有人可以帮忙吗?

I used PropertyModel as the part of my DropDownChoice as following:

    List<String> choices = Arrays.asList(new String[] { "Library", "School Office", "Science Dept" });
    String selected = "Library";

    DropDownChoice<String> serviceDDC = 
            new DropDownChoice<String>("service",  new PropertyModel(this, "choices.0"), choices); 

Somehow I've got this exception thown:

caused by: org.apache.wicket.WicketRuntimeException: No get method defined for class: class com.samoo.tool.pages.CreatePrintingJob expression: choices
    at org.apache.wicket.util.lang.PropertyResolver.getGetAndSetter(PropertyResolver.java:481)
    at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:332)
    at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:242)
    at org.apache.wicket.util.lang.PropertyResolver.getValue(PropertyResolver.java:95)
    at org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:130)
    at org.apache.wicket.Component.getDefaultModelObject(Component.java:1724)

....

I know that there's something wrong with the expression. I've been trying different parameter inputs but it still doesn't work. Could anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅渡 2024-12-25 03:17:45

由于您使用的是 PropertyModel(this, "choices.0"),Wicket 尝试通过 getChoices() 方法通过反射查找名为 choices 的属性声明 PropertyModel 的类的。正如异常所示,此方法似乎不存在于 com.samoo.tool.pages.CreatePrintingJob 中。

另外,如果 0 是索引,您应该使用 [index] 表达式访问它,正如此 JIRA 问题所示:PropertyModel 不支持仅索引属性 ("[0]")

但是,您似乎想要初始化DropDownChoicechoices 的第一个元素。但是,如果您将 DropDownChoice 的模型设置为 PropertyModel(this, "choices.[0"]),Wicket 将按以下方式映射此 DropDownChoice 的选择:

  • 在表单渲染时呈现(预)选择的选项,它将使用 choices 列表中的第一个元素。
  • 在表单提交时存储用户选择的值,它将把选择存储在 choices 列表的第一个位置。

总而言之,表示 DropDownChoice 选择的支持对象将是 choices 列表中的第一个元素。

因此,您可能想要使用一个完全不同的模型,独立于选择列表,作为代表 DDC 选择的支持对象。

List<String> choices = Arrays.asList(new String[] { "Library", "School Office", 
       "Science Dept" });
String selected = "Library";
IModel dropdownModel = new Model<String>(choices[0]);
DropDownChoice<String> serviceDDC = 
        new DropDownChoice<String>("service",  dropdownModel, choices);

您可能会发现以下链接很有用:

Since you're using PropertyModel(this, "choices.0"), Wicket is trying to find a property named choices via reflection through a method getChoices() of the class declaring the PropertyModel. This method doesn't seem to exist in com.samoo.tool.pages.CreatePrintingJob, as the exception is stating.

Also, if that 0 is an index, you should be accessing it with the [index] expression, as this JIRA issue suggests: PropertyModel does not support index only property ("[0]")

However, it seems you want to initialize the DropDownChoice to the first element of choices. But What Wicket will do if you set the DropDownChoice's Model to PropertyModel(this, "choices.[0"]) will be mapping the selection of this DropDownChoice in the following way:

  • At form rendering time to present the (pre)selected choice, it will use the first element in the choices list.
  • At form submission time to store the user selected value, it will store the selection in the first position of the choices list.

Summarising, the backing object representing the DropDownChoice's selection would be the first element in the choices list.

So, you'll probably want to use a whole different Model, independent from the choices list, for the backing object representing the DDC's selection.

List<String> choices = Arrays.asList(new String[] { "Library", "School Office", 
       "Science Dept" });
String selected = "Library";
IModel dropdownModel = new Model<String>(choices[0]);
DropDownChoice<String> serviceDDC = 
        new DropDownChoice<String>("service",  dropdownModel, choices);

You might find the following links useful:

追我者格杀勿论 2024-12-25 03:17:45

您在方法内部声明选择,为了使 PropertyModel 正常工作,您需要在类级别而不是方法级别声明它。正如@Xavi López 指出的那样,表达并不正确,你需要使用选择。[0]

you are declaring choices inside the method, in order to get the PropertyModel to work you need to declare it on a class level not on a method level. As @Xavi López pointed out the espression is not corret you nedd to use choices.[0]

中二柚 2024-12-25 03:17:45

使用IModel而不是PropertyMOdel是个好主意。PropertyModel在重构方面存在很大问题。在我的例子中,我做到了,并且问题得到了正确解决。此外,我还覆盖了我的 Topic 对象的 toString()

topicDropDown = new DropDownChoice<Topic>("topicOptions", new IModel<Topic>() {
        @Override
        public Topic getObject() {
            return top;
        }

        @Override
        public void setObject(Topic t) {
            top = t;
        }

        @Override
        public void detach() {
        }
    }, new LoadableDetachableModel<List<Topic>>() {
        @Override
        protected List<Topic> load() {

            List<Topic> topics = top.getAllTopics();
            return topics;

        }
    });

It is good idea to use IModel instead of PropertyMOdel.PropertyModel has big problems in refactoring. In my cases I did it and the problems solved properly.Also I have override the toString() of my Topic object.

topicDropDown = new DropDownChoice<Topic>("topicOptions", new IModel<Topic>() {
        @Override
        public Topic getObject() {
            return top;
        }

        @Override
        public void setObject(Topic t) {
            top = t;
        }

        @Override
        public void detach() {
        }
    }, new LoadableDetachableModel<List<Topic>>() {
        @Override
        protected List<Topic> load() {

            List<Topic> topics = top.getAllTopics();
            return topics;

        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文