springMVC----ajax以及Json对象

来源:互联网 发布:淘宝店怎么来推广 编辑:程序博客网 时间:2024/05/20 21:27

1.ajax

最常用的形式:

AjaxController.java
@Controller@RequestMapping("/ajaxController")public class AjaxController {    @RequestMapping("/ajax.do")    public void ajax(HttpServletRequest request , HttpServletResponse response) throws IOException {        String name = request.getParameter("name");        if(name.equals("wyh")){            response.getWriter().print("true");        }        else{            response.getWriter().print("false");        }    }}
js
$("#name").change(function () {                $.post("ajaxController/ajax.do",{'name':$("#name").val()},function (data) {                    if(data == "true"){                        alert("你好!");                    }else{                        alert("对不起!");                    }                });            })

2.Json对象转换

a)旧版本:

导入jar包:

jackson-annotations-2.8.0.jar
jackson-core-2.8.1.jar
jackson-databind-2.8.1.jar

配置springmvc-servlet.xml

<!--用于将对象转会为JSON-->    <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/plain;charset=UTF-8</value>            </list>        </property>    </bean>    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="stringConverter" />                <ref bean="jsonConverter"/>            </list>        </property>    </bean>
Controller.java
@Controller@RequestMapping("/jsonController")public class JsonController {    @RequestMapping("/json.do")    //该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等    @ResponseBody    public List<User> viewUser(HttpServletRequest request, HttpServletResponse response){        List<User> users = new ArrayList<>();        users.add(new User(1,"AAA","aaa"));        users.add(new User(2,"BBB","bbb"));        users.add(new User(3,"CCC","ccc"));        return users;    }}
前端接收
$("#viewUser").click(function () {                var html = "";                $.post("jsonController/json.do",function (data) {                    for(var i = 0 ; i < data.length;++i){                        html += "<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].pwd+"</td></tr>"                    }                    $("#content").html(html);                })            })

b)新版本

导入jar包:

jackson-all-1.9.2.jar

配置springmvc-servlet.xml

<mvc:annotation-driven />
注意,一定要在xml中引入如下的配置,否则会报错!
<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        <span style="color:#ff0000;">http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd</span>  ">
其它的东西没有什么变化。关于<mvc:annotation-driven />,这东西初始化了好多Converter,很多就不用像以前那样配置麻烦了















0 0
原创粉丝点击