栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Spring MVC使用JSON的过程与步骤

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC使用JSON的过程与步骤


活动地址:CSDN21天学习挑战赛

目录

 JSON数据交互

 RESTful支持


 JSON数据交互

1.用eclipse创建一个动态web项目,将项目依赖的jar包放到lib目录下:

 2.在WEB-INF目录下创建web.xml,对Spring MVC的前端控制器等信息进行配置。



	chapter13
	
		index.jsp
	
	
	
		
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:springmvc-config.xml
		
		
		1
	
	
		springmvc
		/
	

3.src目录下创建Spring MVC的核心配置文件springmvc-config.xml



	
	
	
	
	
	
	
	
		
		
		
		
	

4.在src目录下创建一个com.ssm.po包,在包内创建一个客户Customer类,该类用于封装User类型的请求参数。

package com.ssm.po;
public class Customer {
	private Integer id;
	private String loginname;
	private String nickname;
	private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String toString() {
		return "Customer [id=" + id + ", loginname=" + loginname + ", nickname=" + nickname + ", password=" + password
				+ "]";
	}
}

5.在webapp目录下创建页面文件json.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


	
	
	测试JSON交互
	
	
	
	
		
登录名:
密    码:

当执行页面中的testJson()函数时,函数会使用JQuery的AJAX方式,将JSON格式的登录名和密码传递到"/testJson"结尾的请求中。所以在webapp目录下创建js文件夹用于存放

jquery-3.5.0.min.js

 6.在src目录下创建com.ssm.controller包,在该包下创建一个用于存放客户操作的控制器类CustomerController

package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.po.Customer;
@Controller
public class CustomerController {
	@RequestMapping("/testJson")
	@ResponseBody
	public Customer testJson(@RequestBody Customer customer){
		System.out.println(customer);
		return customer;
	}

}

使用注解方式定义一个控制器类,并编写了接受和响应JSON格式数据的testJson()方法,在方法中接受并打印了接收到的JSON格式的用户数据,然后返回了JSON格式的用户对象。

方法中的@RequestBody注解用于将前端请求体中的JSON格式数据绑定到形参customer上,@ResponseBody注解用于直接返回Customer对象。

 7.运行json.jsp

 RESTful支持

1.在控制器类CustomerController中编写客户查询方法selectCustomer()

	@RequestMapping(value="/customer/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Customer selectCustomer(@PathVariable("id") Integer id){

		System.out.println(id);
		Customer customer=new Customer();

		if(id==1){
			customer.setLoginname("zyy");
		}
		return customer;
	}

 @RequestMapping(value="/customer/{id}",method=RequestMethod.GET);注解用于匹配请求路径和方式。其中value="/customer/{id}"表示可以匹配到以"customer/{id}"结尾的请求,id为请求中的动态参数;method=RequestMethod.GET表示只接受GET方式的请求。方法中@PathVariable("id")注解则用于接受并绑定请求参数,它可以将请求URL中的变量映射到方法的形参上。如果请求路径为"/customer/{id}",即请求参数中的id和方法形参名称id一样,则@PathVariable后面的("id")可以省略。

 2.在webapp创建restful.jsp,在页面中使用AJAX方式通过输入的客户编号来查询客户信息

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


	
	
	RESTful测试
	
	
	
	
		
客户编号:

4.运行restful.jsp

 源码:

链接:https://pan.baidu.com/s/1ge-iF6GoQ-KQiM_T5Ynw9Q?pwd=5bi6
提取码:5bi6

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1041015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号