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
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
- springMVC----ajax以及Json对象
- springmvc ajax请求以及json
- SpringMVC整合Ajax传JSON对象
- springMVC使用ajax返回json对象,用jquery进行解析.
- ajax跨域请求springmvc传递json对象参数
- SpringMVC如何处理Ajax请求和返回Json对象
- 2.ajax+springmvc+json
- SpringMvc+Ajax+json
- springmvc jquery json ajax
- SpringMVC+ajax+json
- SpringMVC ajax json 415
- Json对象和Json字符串的区别,以及Springmvc接受json字符串类型。
- Json对象和Json字符串的区别,以及Springmvc接受json字符串类型。
- springMVC接收JSON数据转java对象以及Java对象转JSON数据
- SpringMVC 过滤json对象
- SpringMVC接收json对象
- springMVC+ajax返回JSON串
- SpringMVC+ajax返回JSON串
- Android的clipChildren属性
- 11.Javascript中的数据类型转换
- Const类型变量
- @SuppressWarnings注解
- MYSQL的用户变量(@)和系统变量(@@)
- springMVC----ajax以及Json对象
- 计算任意两个定点的最长路径
- 六款APP加上一支笔 让你成为绘画大师
- github windows 客户端 使用教程
- 返回顶部小火箭
- PagerAdapter滑动页面,onpagechangelistener监听滑动
- 获取文件类型和文件名
- opencv 人脸识别 java版------2
- 创建一个规范的iOS工程(xcode8新特性)