SpringMVC 02_day13_解析restful风格参数+ajax提交

MVC分层

1.创建Car类——Model
在这里插入图片描述

package cn.tedu.pojo;
//封装数据 Model
public class Car {

    //{"id":718,"name":"保时捷","type":"Cayman T","color":"红色","price":641000.0}
    private Integer id;
    private String name;
    private String type;
    private String color;
    private double price;
    //constructors
    public Car(){//无参构造,有含参构造需手动创建
    }
    //全参构造
    public Car(Integer id, String name, String type, String color, double price) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.color = color;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

}

2.CarController—Controller

package cn.tedu.controller;

import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//接收请求
@RequestMapping("car")
public class CarController {//http://localhost:8080/car/get
    @RequestMapping("get")
    public String get(){
        System.out.println(10000);
        return "加油干!";
    }

    @RequestMapping("get2")
    public Car get2() {//http://localhost:8080/car/get2
        Car c = new Car(718,"保时捷","Cayman T","红色",641000.0);
        return c;//{"id":718,"name":"保时捷","type":"Cayman T","color":"红色","price":641000.0}
    }
    @RequestMapping("add")//http://localhost:8080/car/add?id
    public void get3(int id){//这里不用int,而用Integer,可以接收没有参数的null值
        System.out.println(id);
    }
    //点击后报错(type=Internal Server Error, status=500).id未输入请求值
    @RequestMapping("add2")
    public void get3(Integer id,String name){//http://localhost:8080/car/add2?id=1&name=名
        System.out.println(id+" "+name);
    }
    //Integer类型可接收null值,不会报错——控制台打印   1 名
    //http://localhost:8080/car/add3?id=1&name=名&color=red&type=BWM&price=9.9
    @RequestMapping("add3")//"http://localhost:8080/car/add3?id=1&name=名&color=red&type=BWM&price=9.9"
    public void get3(Car c){
        System.out.println(c);
    }//Car{id=1, name='名', type='BWM', color='red', price=9.9}
    //SpringMVC可直接接收对象来解析数据
}

在这里插入图片描述
3.View

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<a href="http://localhost:8080/car/get">点我发起请求1</a>
		<a href="http://localhost:8080/car/get2">点我发起请求2</a>
		<a href="http://localhost:8080/car/add">点我发起请求3</a>
		<a href="http://localhost:8080/car/add2?id=1&name=名">点我发起请求get</a>
		<a href="http://localhost:8080/car/add3?id=1&name=名&color=red&type=BWM&price=9.9">点我发起请求get1</a>		
	</body>
</html>

解析restful风格的请求参数

$$!-restful提交数据,数据量少,解析的更快,写法简洁!

href="http://localhost:8080/hi/add?id=10&type=BWM&price=9.9"
href="http://localhost:8080/hi/add2/10/BWM/9.9"

View改造成restful方式

	<body>
		<a href="http://localhost:8080/hi/ha">请下注</a>
		<a href="http://localhost:8080/hi/get">车价格</a>
		<a href="http://localhost:8080/hi/add?id=10&type=BWM&price=9.9">点我提交数据1</a>
		<a href="http://localhost:8080/hi/add2/10/BWM/9.9">点我提交数据2</a>
	</body>

Controller改造成restful方式

	//普通GET方式提交http://localhost:8080/car/add?id=10&name=BWM&price=9.9
   @RequestMapping("add")
    public String get3(Integer id,String name,Double price){
        return id+name+price;//10BWM9.9
    }
    //优化get方式提交数据的方式restful:http://localhost:8080/add2/10/BWM/9.9
    @RequestMapping("add2/{id}/{name}/{price}")
    //{id}是占位符,表示这个参数的位置
    //@PathVariable用来获取{}中间的值
    public void add2( @PathVariable Integer id,
                      @PathVariable String name,
                      @PathVariable Double price){
        System.out.println(id+" "+name+" "+price);//10 BWM 9.9
    }

改造成ajax提交请求数据

View改造成ajax方式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../jquery-1.8.3.min.js"></script>
		<script>
			function a(){//文档就绪事件
					$.ajax({ //发起Ajax请求数据
						type: "GET",
						url: "http://localhost:8080/hi/get",
						contentType: "application/json;charset=utf-8",
						data:{		//拼接的参数
							"id":"10",
							"name":"BWM",
							"price":"9.9"
						},
						 dataType: "json",
						success: function(data) {//返回的结果
							console.log(data);//返回值的是对象数组
							console.log(data.id);//打印对象的属性
							alert(100);
						}
					})	
			}
		</script>
	</head>
	<body>
		<a onclick="a();" href = "#" >ajax数据</a>
	</body>
</html>

Controller类,加一个跨域的注解

package cn.tedu.controller;

import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hi")
@CrossOrigin//解决跨域问题
public class CarController {
    @RequestMapping("ha")//http://localhost:8080/hi/ha
    public String get(){
        System.out.println("下个锤子");
        return "下个锤子";
    }
    @RequestMapping("get")//http://localhost:8080/hi/get
    public Car get2(){
        Car c = new Car();
        c.setId(100);
        c.setPrice(19.9);
        return  c;
    }
    //普通GET方式提交http://localhost:8080/car/add?id=10&name=BWM&price=9.9
    @RequestMapping("add")
    public String get3(Integer id,String name,Double price){
        return id+name+price;
    }
    //优化get方式提交数据的方式restful:http://localhost:8080/add2/10/BWM/9.9
    @RequestMapping("add2/{id}/{name}/{price}")
    //{id}是占位符,表示这个参数的位置
    //@PathVariable用来获取{}中间的值
    public void add2( @PathVariable Integer id,
                      @PathVariable String name,
                      @PathVariable Double price){
        System.out.println(id+" "+name+" "+price);
    }
}

成功:
在这里插入图片描述
失败: 没加下面的解决跨域的注解
@CrossOrigin//解决跨域问题
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值