springmvc05——对JSON的支持

JSON

格式:{“key”:“value”}
js中
JSON中
JSON字符串

自身对JSON不支持(你返回的对象类型不是JSON对象的),但他通过一个转化接口,叫做Jackson的包,进行JSON的开发。三个包。

我们做项目,做一个登陆的案例,使用ajax开发
新建spring03java项目,导包,一共十九个包
首先在web.xml中配置,直接复制上一个例子的就可以,但我们现在使用"/"来拦截资源
在这里插入图片描述
他会连静态资源也拦截,后面会再进行配置

然后进行配置spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!--注解驱动-->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<context:component-scan base-package="com.cbb.controller"></context:component-scan>
	
	
</beans>

注解驱动,会出现问题,先放过去
然后创建一个index.jsp文件
添加需要的js文件
js文件
在index.jsp上添加上js文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src = "${pageContext.request.contextPath}/js/jquery-3.1.1.min.js"></script>
</head>
<body>
	登录页
</body>
</html>

测试不管用,出现了问题
因为静态资源被拦截,所以我们还需要配置spring.xml文件(添加一个内容)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!--注解驱动-->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<context:component-scan base-package="com.cbb.controller"></context:component-scan>
	
	<!-- 处理静态资源被defaultservlet处理 -->
	<mvc:default-servlet-handler/>
	
</beans>

第一步完成,编写类

有两种方式,第一种,你返回的不是json类型的,你需要上面的包帮你转成json类型的
还有一种,就是你返回的就是json类型的,就不需要进行转化了(使用阿里巴巴的functionjson????发音差不多的)

user类

package com.cbb.pojo;


public class User {

	private Integer id;
	
	private String username;
	
	private String password;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(Integer id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}
	
	
}

controller类(分析,使用Jackson包,把数据转换成json格式的。能被转换的是String字符串,map类型)。
我们需要新的注解:
@ResponseBody
使用这个才能进行异步开发。作用是:将处理器返回的数据转换为
指定的格式,json格式的。
通过response进行返回

package com.cbb.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbb.pojo.User;



@Controller
@RequestMapping("user")
public class UserController {
	
	/** 
	 * 方法描述:异步的用户登录功能,数据是通过ajax进行提交的,而非from表单提交
	 * @param user
	 */
	@RequestMapping(value="login")
	@ResponseBody	//使用这个才能进行异步开发。作用是:将处理器返回的数据转换为指定的格式,json格式的。
					//通过response进行返回
	public Map<String, Object> login(User user){
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "登录方法访问成功");
		return map;
	}
}

我们开始写功能:登录

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src = "${pageContext.request.contextPath}/js/jquery-3.1.1.min.js"></script>
</head>
<body>
	 <!-- <form action=""> --> <!--如果有form表单会一直在刷新-->
		用户名:<input type="text" id="username"><br/>
		密码:<input type="text" id="password"><br/>
		<button onclick = "login()">登录</button>
	 <!-- </form>  -->
</body>
<script type="text/javascript">
	function login(){
		//通过jquery获取值
		var um = $("#username").val();
		var password = $("#password").val();
		
		//使用ajax进行提交
		$.ajax({
			url:"${pageContext.request.contextPath}/user/login",//路径,是字符串
			type:"post",
			data:{
				username : um,
				password : password
				
			},
			dataType:"json",//定义返回类型的数据格式为json格式
			success : function(data){
				//请求成功
				alert(data.msg)
			}
		})
	}

</script>
</html>

有form表单会一直在刷新,所以我们把他注释掉,一直刷新的效果未能实现,记住这个知识点就可以。
alert里面要获取到数据,就需要alert(data.msg)如果只是alert(data),那么就会显示为空。
然后我们就可以获取实验效果了
在这里插入图片描述
这就是springmvc对json的支持

还有就是直接返回json类型字符串

	/** 
	 * 方法描述:异步的用户登录功能,数据是通过ajax进行提交的,而非from表单提交

	 */
	@RequestMapping(value="login")
	@ResponseBody	//使用这个才能进行异步开发。作用是:将处理器返回的数据转换为指定的格式,json格式的。
					//通过response进行返回
	public String login(User user){
		JSONObject json = new JSPNObject();
		json.put("msg", "登录方法访问成功");
		return json.toJSONString;
	}

如果使用的是按钮来进行提交的话,如果有form表单,他会将form表单一起提交,你的form表单中即使没有输入值,也会默认使用当前地址进行提交,这样就会出现同时请求两次的情况。
ajax在请求的过程中,还没有得到响应,就被打断了,就会报错。(操作会执行)在这里插入图片描述
解决方法有两个,第一种把from表单去掉
第二种,在表单上加上一个东西,阻止他进行提交。
在这里插入图片描述
这样就可以了

END

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值