springmvc json数据传输

来源:互联网 发布:js重置表单作用 编辑:程序博客网 时间:2024/05/18 02:38

1、springmvc进行json交互


1)、请求json、输出json,要求请求的是json串,所以在前端页面中需要将请求的内容转成json,不太方便。

2)、请求key/value、输出json。此方法比较常用。

2、环境准备

2.1 加载json转换的jar包


2.2 配置json转换器

在springmvc-servlet.xml的注解适配器中加入messageConverters

<!--注解适配器 -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

       <propertyname="messageConverters">

       <list>

       <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>

       </list>

       </property>

    </bean>


注意:如果使用<mvc:annotation-driven />则不用定义上边的内容。

3、json交互测试

3.1、输入json串,输出是json串
3.1.1、jsp页面
使用jquery的ajax提交json串,对输出的json结果进行解析。

3.2 controller



3.3 测试结果




4、输入key/value,输出是json串

4.1、jsp页面

使用jquery的ajax提交key/value串,对输出的json结果进行解析。

4.2、controller


4.3、测试

0 0
原创粉丝点击