美文网首页SpringMVC笔记
SpringMVC json配置总结

SpringMVC json配置总结

作者: RX_AI | 来源:发表于2018-09-17 10:48 被阅读47次

后端需要做的事情:

【1】springmvc框架需要引入的jar包

必须使用的3个jar文件

【2】第二步:

配置解析器

···

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

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

        <property name="messageConverters">

            <list>

                <bean class="org.springframework.http.converter.StringHttpMessageConverter">

                    <property name="supportedMediaTypes">

                        <list>

                            <value>text/html; charset=UTF-8</value>

                            <value>application/json;charset=UTF-8</value>

                        </list>

                    </property>

                </bean>

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

                    <property name="supportedMediaTypes">

                        <list>

                            <value>text/html; charset=UTF-8</value>

                            <value>application/json;charset=UTF-8</value>

                        </list>

                    </property>

                </bean>

            </list>

        </property>

    </bean>

···swift

【3】第三步

编写controler类的编写

可以直接返回容器,框架会自动进行json串的处理工作。

必须使用注解告诉一下框架这个一个json

~~~

@RequestMapping("/json")

@ResponseBody

public List<User> json() {

~~~

前段需要做的事情:

使用Ajax的Jquery框架进行调用

~~~

$(function() {

$("#btn").click(

function() {

$.post("json", function(data) {

var html = "";

for (var i = 0; i < data.length; i++) {

html += "<tr><td>" + data[i].loginname + "</td>"

+ "<td>" + data[i].username + "</td>"

+ "<td>" + data[i].password + "</td></tr>"

}

$("#content").html(html);

});

});

});

~~~

需要知道后端的具体url,第二个参数是URL的参数,第三个参数是回调data函数,

相关文章

网友评论

    本文标题:SpringMVC json配置总结

    本文链接:https://www.haomeiwen.com/subject/pcdknftx.html