目录
第一节 SpringMVC中Jquery提交Json数据
1.1 ResponseBody和RequestBody简介
- @ResponseBody把后台pojo转换json对象,返回到页面。
- @RequestBody接收前台json数据,把json数据自动封装为javaBean
1.2 案例:请求和响应都是json数据
第一步:导入json的jar
第二步:在SpringMVC配置文件中添加个json转换器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<!--配置json转换器-->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</property>
</bean>
第三步:写个jsp页面,提交json请求参数
<%--
Created by IntelliJ IDEA.
User: shuyy
Date: 2020/10/9
Time: 13:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
function register() {
//1.获取输入的参数
var name = document.getElementById('name').value;
var password = document.getElementById('password').value;
//2.封装json数据
var jsonData = {
"name":name,"password":password};
//3.封装成json对象
var jsonObj = JSON.stringify(jsonData);
//4.设置请求路径
var url = '${pageContext.request.contextPath}/stu/save.do';
$.ajax({
type:'post',
url:url,
contentType:'application/json;chartset=utf-8',
data:jsonObj,
success:function (respData) {
alert(respData.name +":"+ respData.password);
}
});
/*$.post(url,jsonObj,function (respData) {
alert(respData.name +":"+ respData.password);
},'application/json;chartset=utf-8');*/
}
</script>
<head>
<title>学生注册页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/stu/save.do" method="post">
用户名:<input type="text" name="username" id="name"><br>
密码: <input type="text" name="password" id="password"><br>
<input type="button" value="json提交" onclick="register();">
<input type="submit" value="普通提交">
</form>
</body>
</html>
第四步:后台处理
- 提供一个学生类(提供无参构造、get/set、toString)
- 提供一个toReg,访问stu下的register.jsp,提供一个save,查看信息
package com.it.web.controller;
import com.it.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @ClassName UserController
* @Author shuyy
* @Date 2020/10/5 20:09
**/
@Controller//相当于在Spring中配置了一个<bean class="控制器的路径"
@RequestMapping("/stu")
public class StudentController {
//转发到不同控制器的方法
@RequestMapping("/test")
public String test(@RequestParam(value = "uid",required = true,defaultValue = "1")int uid){
System.out.println(uid);
return "redirect:/user/list.do";
}
@RequestMapping("/toReg")
public String toReg(){
return "/stu/register";
}
@RequestMapping("/save")
//@RequestBody将前台json数据转成javabean对象
//@ResponseBody将后台javabean对象转换成json返回到页面
public @ResponseBody Student save(@RequestBody Student stu){
System.out.println(stu);
return stu;
}
}
第五步:测试
- 普通提交
- json提交
1.3 案例:只有响应是json数据【常用】
第一步:写一个表单
第二步:后台处理
第三步:测试
第二节 SpringMVC多视图配置
- 多视图是一个可以返回json/xml等格式数据的一种方法(客户端想要json格式的数据就返回json,想要xml格式的数据就返回xml)
第一步:导入支持xml格式的jar包
第二步:配置支持多视图
<!--5.配置多视图,既可以返回json,也可以返回xml-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<!-- 配置支持媒体类型 -->
<property name="contentNegotiationManager">
<bean
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"></entry>
<entry key="xml" value="application/xml"></entry>
</map>
</property>
</bean>
</property>
<!-- 指定默认视图 -->
<property name="defaultViews">
<!-- 支持多个视图 -->
<list>
<!-- 对json格式视图支持 -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
<!-- xml格式视图支持 -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.it.model.Student</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
第三步:在控制器中写方法
第四步:测试
- 直接访问是访问不到的,因为配置了拦截所有.do后缀请求,其它方式不通过
- 可以通过之前配置的restful访问
- 直接访问xml也会报错,因为它不知道你的根标签是什么
- 还需要在学生(所要配置的)类上声明一个注解
- 测试一下,返回的数据是一个集合
- json没问题
- 但是xml就报错了(无法将集合转成一个对象)(需要手动转换或其它方法)