[英]FreeMarker and 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媒体类型或表示形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.