2h SpringMVC-Json.txt
UP 返回
1 ResponseBody和RequestBody
@ResponseBody把后台pojo转换json对象,返回到页面。
@RequestBody接受前台json数据,把json数据自动封装javaBean
2.代码实现(█项目路径参07中项目)
2.1 导入json的jar
jackson-core-asl-1.9.13.jar 位置:F:\BaiduNetdiskDownload\2019年4月黑马程序员教程\01-黑马IDEA版本Java基础+就业课程\4.框架\Java开发工具\jar包\jackson
jackson-mapper-asl-1.9.13.jar
▶导入以后报错,发现少了以下几个包,都要导入:
jackson-core-2.10.0.jar 位置:D:\EnvironmentExtends\external_jar
jackson-databind-2.10.0.jar
jackson-annotations-2.10.0.jar
2.2 在DispatcherServlet-servlet.xml中添加json转换器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!--▶配置json的转换器,告诉springmvc使用jackson来转换json-->
<property name="messageConverters"><!--如果只有一个类下面直接配bean就可以了。否则用集合<list><bean></bean><bean></bean></list>-->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</property>
</bean>
2.3 复制js文件
在web下新建js文件夹,复制js文件进去(一般WEB-INF里的东西是不希望别人直接访问到的,其他的可以直接放到web下面,比如js)
jquery-1.8.3.js 位置:F:\BaiduNetdiskDownload\2019年4月黑马程序员教程\01-黑马IDEA版本Java基础+就业课程\4.框架\05.SpringMVC
2.4 jsp页面
■register.jsp
<html>
<head>
<title>Title</title>
<%--▶引入js--%>
<script src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
<script>
function register() {
var url='${pageContext.request.contextPath}/stu/save.do';
var name=$('#name').val();
var sex=$('#sex').val();
var jsonObj={name:name,sex:sex};
var parameters=JSON.stringify(jsonObj);//▶json对象转化为字符串
//▶jQuery提交json数据
$.ajax({
url:url,
type:'post',
contentType:'application/json;charset=utf-8', //▶告诉提交方式
data:parameters,
success:function (respData) {
console.log(respData);
}
});
}
</script>
</head>
<body>
<%--▶浏览器里可以看到默认的表单提交格式:application/x-www-form-urlencoded 对应数据是:username=ss&sex=ss--%>
<%--▶如果想要Jason提交,就得告诉浏览器格式:application/json 对应数据:{"name":"ss","sex":"ss"}--%>
<form action="${pageContext.request.contextPath}/stu/save.do" method="post">
学生名:<input id="name" type="text" name="username"><br>
性别:<input id="sex" type="text" name="sex"><br>
<input type="button" value="提交json" onclick="register();"><%--▶onclick里的;可加可不加--%>
<input type="submit">
</form>
</body>
</html>
2.5 后台
■Controller
@Controller
@RequestMapping("/stu")
public class StudentController {
@RequestMapping("/toReg") //访问/stu/toReg.do进入2.4中的页面
public String test2(){
return "stu/register";
}
/**
* ▶RequestBody:把json数据转化成模型对象(默认的必须是username=ss&sex=ss才能自动得到参数对象,不加的话拿到的都是null+)
* ▶ResponseBody:返回json数据,把对象转成json字符串返回客户端(前段ajax会自动把json字符串转为对象,可以从浏览器中看出来)
*/
@RequestMapping("/save")
public @ResponseBody Student save(@RequestBody Student stu){
System.out.println(stu);
return stu;
}
}
2.6 以上是请求响应都是json格式。如果只有一边是json数据,去除掉对应的注释改改就好了。另外项目需要创建Student模型类,不然跑不起来
3.SpringMVC多视图
多视图是一个方法可以返回json/xml等格式的数据
3.1 导入xml格式支持的jar
spring-oxm-XXX.jar 位置在jar中
3.2 在DispatcherServlet-servlet.xml中添加一个配置支持多视图
<!--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>
<!-- 对josn格式视图支持 -->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
<!-- xml格式视图支持 -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<!--▶配置哪些类可以转为xml-->
<value>com.dm.backoffice.model.Student</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
3.3 StudentController中添加一个对应的控制方法:
@RequestMapping("/get")
public Student get(){
Student stu=new Student();
stu.setName("dsds");
stu.setSex("male");
return stu;
}
3.4 Student类添加一个标签
@XmlRootElement(name="student")
public class Student {
private String name;
private String sex;
}
3.5 在浏览器中访问
http://localhost:8080/rest/stu/get.xml
http://localhost:8080/rest/stu/get.json
两种url都可以被接收到对应的方法中去,且返回的页面可以根据.xml .json自动转为页面内容
DOWN 返回