仅从 Spring MVC 3 控制器返回字符串消息 [英] Return only string message from Spring MVC 3 Controller
问题描述
谁能告诉我如何从控制器返回字符串消息?
Can any one tell me how I can return string message from controller?
如果我只是从控制器方法返回一个字符串,那么 spring mvc 将其视为一个 jsp 视图名称.
If i just return a string from a controller method then spring mvc treating it as a jsp view name.
推荐答案
用 @ResponseBody
在控制器中注释你的方法:
Annotate your method in controller with @ResponseBody
:
@RequestMapping(value="/controller", method=GET)
@ResponseBody
public String foo() {
return "Response!";
}
来自:15.3.2.6 使用@ResponseBody
注解映射响应体:
From: 15.3.2.6 Mapping the response body with the @ResponseBody
annotation:
@ResponseBody
注释 [...] 可以放在方法上,指示返回类型应直接写入 HTTP 响应正文(而不是放在模型中,或解释作为视图名称).
The
@ResponseBody
annotation [...] can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).
这篇关于仅从 Spring MVC 3 控制器返回字符串消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!