java - FreeMarker 和 HTML
问题描述
我正在尝试使用FreeMarker模板,这是我的代码:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{queue}")
public String test(@PathParam("queue") String qy,
@Context HttpServletRequest request) {
Map<String,Object> model = new HashMap<String,Object>();
String listTemplate = "<HTML><HEAD><STYLE>${css}</STYLE></HEAD><BODY><UL> the queue includes : ${queues}</UL></BODY></HTML>";
String listCss = inMemService.getWebCss("list");
model.put("css", listCss);
String result = null ;
IQueue queue = com.myproject.hc.Main.getHzInstance().getQueue(qy);
// Build the data-model
Object[] array = queue.toArray();
int size = queue.size();
int i;
for (i = 0; i < size ; i ++) {
temp = temp + " " + array [i] + " ";
}
model.put("queues", temp);
Template template ;
try {
template = new Template("process", new StringReader(
listTemplate), new Configuration());
Writer stringWriter = new StringWriter();
template.process(model, stringWriter);
stringWriter.flush();
result = stringWriter.toString();
} catch (IOException e) {
} catch (TemplateException e) {
}
return result;
}
但是,当我添加到队列中并转到 时@Path
,我得到以下输出,它不能识别为HTML
:
<HTML><HEAD><STYLE>body {background-color: lightgrey;} h1 {color: black;text-align: left;} p {font-family: verdana;font-size: 20px;}</STYLE></HEAD><BODY><UL> the queue includes : LA NYC </UL></BODY></HTML>
解决方案
您的 HTML 很好,您需要在返回 HTML(而不是 JSON)的代码中声明。
您应该将@Produces(MediaType.APPLICATION_JSON)替换为:
@Produces({MediaType.TEXT_HTML})
@Produces 注释用于指定资源可以生成并发送回客户端的 MIME 媒体类型或表示。
推荐阅读
- php - 无法在 Kubernetes 上将 HTTP 与 php 映像一起使用
- r - 一个简单的可重现示例,用于在 R 中的自定义函数中将参数传递给 data.table
- security - 每个用户的秘密 JWT
- java - Sleeping Professor 问题代码跟踪中的信号量?
- d3.js - Understanding MERGE in context of force graph
- javascript - 简单的网页抓取 Javascript 查询选择器问题
- javascript - How to get ESLint --fix to also run prettier --write
- java - The method startFlow(FlowLogic, InvocationContext) in the type FlowStarter is not applicable for the arguments
- c++ - 合并 2 个已排序的链表。必需的成员函数 bool Merge(List342 &list1):
- python - 调用脚本可以从终端工作,但不能从 python subprocess.call