gpt4 book ai didi

java - Wicket 标签在 setDefaultModelObject 上没有正确更改

转载 作者:行者123 更新时间:2023-11-30 08:48:21 25 4
gpt4 key购买 nike

我使用 PropertyModel 类和 setDefaultModelObject() 方法编写一个在单击页面链接时更改标签的页面。我希望从“John Doe”更改为“Jane Smith”。但它更改为 com.mycompany.myproject.ScoreBean@31f9b85e_

这是 Wicket 的正确行为吗?还是我弄错了什么?我正在使用 Wicket 7.0.0。

示例页面.java

package com.mycompany.myproject;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.PropertyModel;

public class ExamplePage extends WebPage {
public ExamplePage() {
ScoreBean johnScore = new ScoreBean();
johnScore.setName("John Doe");
johnScore.setScore(79);

final ScoreBean janeScore = new ScoreBean();
janeScore.setName("Jane Smith");
janeScore.setScore(84);

final Label label = new Label("label", new PropertyModel<>(johnScore, "name"));
add(label);

Link link = new Link("link") {
@Override
public void onClick() {
label.setDefaultModelObject(janeScore);
}
};
add(link);
}
}

ExamplePage.html

<!DOCTYPE html>
<html lang="en" xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<span wicket:id="label"></span>
<a wicket:id="link">Click me</a>
</body>
</html>

ScoreBean.java

package com.mycompany.myproject;

import java.io.Serializable;

public class ScoreBean implements Serializable {
private String name;
private Integer score;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getScore() {
return score;
}

public void setScore(Integer score) {
this.score = score;
}
}

ExamplePageTest.java

package com.mycompany.myproject;

import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;

public class ExamplePageTest
{
private WicketTester tester;

@Before
public void setUp()
{
tester = new WicketTester(new WicketApplication());
}

@Test
public void labelChangedCorrectly()
{
tester.startPage(ExamplePage.class);
tester.assertRenderedPage(ExamplePage.class);

tester.clickLink("link");
tester.assertLabel("label", "Jane Smith");
}
}

测试结果。

12:48:16.883 [main] INFO  org.apache.wicket.Application - [WicketTesterApplication-77ef7482-95ae-4e94-b274-1e9fcfb961be] init: Wicket core library initializer
12:48:16.887 [main] INFO org.apache.wicket.RequestListenerInterface - registered listener interface [RequestListenerInterface name=IBehaviorListener, method=public abstract void org.apache.wicket.behavior.IBehaviorListener.onRequest()]
12:48:16.891 [main] INFO org.apache.wicket.RequestListenerInterface - registered listener interface [RequestListenerInterface name=IFormSubmitListener, method=public abstract void org.apache.wicket.markup.html.form.IFormSubmitListener.onFormSubmitted()]
12:48:16.892 [main] INFO org.apache.wicket.RequestListenerInterface - registered listener interface [RequestListenerInterface name=ILinkListener, method=public abstract void org.apache.wicket.markup.html.link.ILinkListener.onLinkClicked()]
12:48:16.893 [main] INFO org.apache.wicket.RequestListenerInterface - registered listener interface [RequestListenerInterface name=IOnChangeListener, method=public abstract void org.apache.wicket.markup.html.form.IOnChangeListener.onSelectionChanged()]
12:48:16.894 [main] INFO org.apache.wicket.RequestListenerInterface - registered listener interface [RequestListenerInterface name=IResourceListener, method=public abstract void org.apache.wicket.IResourceListener.onResourceRequested()]

org.junit.ComparisonFailure:
Expected :Jane Smith
Actual :com.mycompany.myproject.ScoreBean@31f9b85e
<Click to see difference>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at org.apache.wicket.util.tester.WicketTester.assertLabel(WicketTester.java:603)
at com.mycompany.myproject.ExamplePageTest.labelChangedCorrectly(ExamplePageTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


Process finished with exit code 255

最佳答案

错误可能出在点击处理方法中:

label.setDefaultModelObject(janeScore);

其中 janeScore 不是 PropertyModel 而是一个常规对象。在这种情况下,将调用 Object 上的 toString 方法来解释输出。

您必须为 janeScore 初始化另一个 PropertyModel 或者直接设置所需的值:

label.setDefaultModelObject(janeScore.getName());

请注意,janeScore 与页面保持一致。如果你有一个真正的数据源(数据库),你必须切换到一个LoadableDetachableModel

关于java - Wicket 标签在 setDefaultModelObject 上没有正确更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32006127/

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