第七讲 Spring MVC中AJAX和JSON的处理
来源:互联网 发布:淘宝申请退换货流程 编辑:程序博客网 时间:2024/06/03 15:22
一、使用HttpServletResponse来处理-不需要配置解析器
@RequestMapping(value = "/ajax", method=RequestMethod.POST)
public void ajax(String name, HttpServletResponse response) throws IOException {
if("siggy".equals(name)) {
response.getWriter().print("true");
}
else {
response.getWriter().print("false");
}
}
<script src="./jquery/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$.post("ajax.do", {'name':$("#txtName").val()}, function(data) {
alert("数据:" + data);
});
});
});
</script>
</head>
<body>
用户名:<input id="txtName" type="text" /><br><br>
<button>向页面发送 HTTP POST 请求,然后获得返回的结果</button><br><br>
</body>
二、Spring MVC处理JSON数据(用异步响应回去的一般都是JSON格式的数据)
- 导入jar包:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
- 配置JSON转换器:
<!-- json配置 -->
<!-- 用于将对象转换为 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>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
- Controller代码:
@Controller
publicclassJsonController {
@RequestMapping(value ="/json")
@ResponseBody
publicList<User> json() {
List<User>list= newArrayList<User>();
list.add(newUser(1,"zhangsan","男"));
list.add(newUser(2,"nico","female"));
list.add(newUser(3,"jackson","男"));
returnlist;
}
}
JSP页面代码
<scriptsrc="./jquery/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function() {
$.post("json.do",function(data) {
varhtml = "";
for(vari = 0; i < data.length; ++i) {
html +="<tr><td>"+data[i].id
+"</td><td>"+data[i].name
+"</td><td>"+data[i].sex+"</td></tr>";
}
$("#content").html(html);
});
});
});
</script>
</head>
<body>
<inputid="button"type="button"value="获取数据"/><br>
<tablewidth="80%"align="center">
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
</tr>
<tbodyid="content">
</tbody>
</table>
</body>
实体类代码
publicclassUser {
privateintid;
privateStringname;
privateStringsex;
publicUser() {
}
publicUser(intid, Stringname, Stringsex) {
super();
this.id=id;
this.name=name;
this.sex=sex;
}
//下面省略get、set方法
阅读全文
0 0
- 第七讲 Spring MVC中AJAX和JSON的处理
- Spring MVC、Ajax和JSON的交互
- Spring mvc 中ajax的处理
- Spring MVC中ajax的处理
- Spring Mvc ajax和json数据格式的配置
- 第七讲 图片的调整和处理
- Spring MVC中ajax返回json数据
- spring-mvc+ajax+json
- Spring MVC Ajax Json
- spring mvc处理json
- Spring MVC Controller与jquery ajax请求处理json
- Spring MVC Controller与jquery ajax请求处理json
- spring mvc 处理 Ajax
- mvc 中 JSON,ajax
- Spring MVC 对 json数据的处理
- MVC中处理Json和JS中处理Json对象
- MVC中处理Json和JS中处理Json对象
- MVC中处理Json和JS中处理Json对象
- 简述slice和splice区别
- C语言程序设计(26)
- 神经网络梯度与归一化问题总结+highway network、ResNet的思考
- bugku ctf平台之sql注入1 write up
- DOM
- 第七讲 Spring MVC中AJAX和JSON的处理
- Android 项目从零到上线的全过程
- Linux vmstat命令实战详解
- 《UNIX网络编程 卷1》 笔记: 使用poll函数的单进程TCP回射服务器程序
- id生成策略-(xml、annotation)-4
- 知识图谱的笔记之一(1)
- 一步步学习kotlin for android(一)配置kotlin
- Java基础
- ADAS(2) 各功能模块介绍