java - 单击 AjaxLink 时如何显示标签组件
问题描述
单击 Ajaxlink 时,我需要显示 JSON 文件中的数据。我已经实现了以下不起作用的代码。如果我犯了任何错误,请更正我的代码。(是否可以在 AjaxLink 中添加标签)
提前致谢。
AjaxLink<Void> jsonData = new AjaxLink<Void>("jsonData") {
@Override
public void onClick(AjaxRequestTarget target) {
File jsonFile;
try {
jsonFile = new File(fileLocation);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readValue(jsonFile, JsonNode.class);
Label jsonLabel = new Label("jsonLabel",
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
jsonLabel.setOutputMarkupId(true);
jsonLabel.setOutputMarkupPlaceholderTag(true);
target.add(jsonLabel);
addOrReplace(jsonLabel);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
add(jsonData);
html:
<div>
<a wicket:id="jsonData" class="text-white">View Template</a>
<pre wicket:id="jsonLabel" class="text-white bg-dark"> </pre>
</div>
解决方案
您需要将 Label 添加为 AjaxLink 的兄弟:
final Label jsonLabel = new Label("jsonLabel", Model.of(""));
jsonLabel.setOutputMarkupId(true);
AjaxLink<Void> jsonData = new AjaxLink<Void>("jsonData") {
@Override
public void onClick(AjaxRequestTarget target) {
File jsonFile;
try {
jsonFile = new File(fileLocation);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readValue(jsonFile, JsonNode.class);
// just update the Label's model and re-paint it
jsonLabel.setModelObject(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
target.add(jsonLabel);
} catch (Exception e) {
e.printStackTrace();
}
}
};
add(jsonData, jsonLabel);
你的 HTML 没问题。
推荐阅读
- selenium - 如何从步骤定义文件返回将要执行的当前方法
- javascript - 在 chart.js 中使用条形上方的粘性数字
- asp.net-core - 自定义 ConfigurationDbContext 时更改 IdentityServer 上的架构
- tensorflow - 如何将词汇文本文件传递给模型并在谷歌云平台机器学习引擎上进行训练
- php - |Laravel 加密| 抓取数据库中的多个条目时抛出“有效负载无效”错误,但抓取一个条目时不会
- python - 动态更改回溯期
- c - 在内核自定义中访问 usr/src/linux 时遇到问题
- xcode - Xcode 在加载 .xcodeproj 后立即关闭
- scala - 如何使用没有类型别名的 Scala 猫对 Either 进行排序(请参阅 Herding cats)
- vim - 打开 Vim 时隐藏文件名、行和字符数