仅从Spring MVC 3 Controller返回字符串消息 [英] Return only string message from Spring MVC 3 Controller

查看:122
本文介绍了仅从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映射响应正文注释


@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 Controller返回字符串消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆