下面是官方文档对于@responsebody注解的解释:
?
1
2
3
4
5
6
7
8
9
10
11
12
|
mapping the response body with the @responsebody annotation
the @responsebody annotation is similar to @requestbody . this 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). for example:
@requestmapping (path = "/something" , method = requestmethod.put)
@responsebody
public string helloworld() {
return "hello world" ;
}
the above example will result in the text hello world being written to the http response stream.
as with @requestbody , spring converts the returned object to a response body by using an httpmessageconverter. for more information on these converters, see the previous section and message converters.
|
@resopnsebody注解能够 直接把 控制器返回变量(string)直接 返回给浏览器,也可以通过配置 后,把 对象 序列化成json数据返回给浏览器!如果为 null 就会返回空白。
怎么配置呢 ?需要配置messageconverter:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<bean
class = "org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter" >
<property name= "messageconverters" >
<list>
<ref bean= "mappingjackson2httpmessageconverter" />
</list>
</property>
</bean>
<bean id= "mappingjackson2httpmessageconverter"
class = "org.springframework.http.converter.json.mappingjackson2httpmessageconverter" >
<property name= "supportedmediatypes" >
<list>
<value>text/html;charset=utf- 8 </value>
<value>text/json;charset=utf- 8 </value>
<value>application/json;charset=utf- 8 </value>
</list>
</property>
</bean>
|
下面贴出在官方文档中的位置:
这个需要jackson jar包支持,需要 jackson-annotations,jackson-core,jackson-databind三个包,:
控制器代码:
?
1
2
3
4
5
6
7
|
@requestmapping ( "house/classmanager/addbyajax" )
@responsebody
public hanblog_class classmanager_addbyajax(httpservletrequest request){
if (request.getsession().getattribute( "hanblog_uid" )== null ) return null ;
hanblog_class objclass= new hanblog_class();
return objclass;
}
|
jquery代码:
?
1
2
3
4
5
6
7
8
9
|
//|增加
$( "#hanblog_add_btn" ).click(function(){
var classname=$( "#add_input_name" ).val();
var classintroduction=$( "#add_input_introduction" ).val();
alert( "分类名称:" +classname+ "分类介绍:" +classintroduction);
$.get( "<c:url value=" /house/classmanager/addbyajax. do " />" ,function(result){
alert(result);
});
});
|
运行返回例子:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/u012997311/article/details/51982461
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 27
-
2025-05-27 20
-
2025-05-27 53
-
2025-05-25 32
-
2025-05-25 31
热门评论