Spring MVC 3 Return内容类型:文本/纯文本 [英] Spring MVC 3 Return Content-Type: text/plain
问题描述
我想在页面上显示简单文本,因此我想将 Content-Type
返回为 text / plain
。
I want to display simple text on a page and as such I want to return the Content-Type
as text/plain
.
使用下面的代码,我在页面上看到纯文本,但是返回 Content-Type
仍然是 text / html
。
Using the code below, I see plain text on the page, however the return Content-Type
is still text/html
.
我该如何解决?
注意:我正在使用带有Spring MVC的Tiles。返回的 m.health指向图块def,该图块def映射到仅包含下面1行的health.jsp。
NOTE: I'm using Tiles with Spring MVC. The returned "m.health" points to a tiles def that maps to a health.jsp which only contains the 1 line below.
更新说明 >:我无法控制HTTP标头请求中的 Content-Type
或 Accept
值。不管收到什么样的请求,我都希望我的响应返回 text / plain
。
UPDATE NOTE: I have no control over the Content-Type
or Accept
values in the HTTP Header request. I want my response to return text/plain
no matter what kind of request comes in.
Controller:
Controller:
@RequestMapping(value = "/m/health", method = RequestMethod.GET, headers = "Accept=*")
public String runHealthCheck(HttpServletResponse response, HttpServletRequest request, Model model) throws Exception {
model = executeCheck(request, response, TEMPLATE, false, model);
model.addAttribute("accept", "text/plain");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
return "m.health";
}
JSP:
$ {状态}
${status}
推荐答案
如果您使用@ResponseBody附加注释您的方法:
It should work if you annotate your method additionally with @ResponseBody:
@RequestMapping(value = "/",
method = RequestMethod.GET)
@ResponseBody
public String plaintext(HttpServletResponse response) {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
return "TEXT";
}
这篇关于Spring MVC 3 Return内容类型:文本/纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!