cgb2106-day14

一,SpringMVC解析restful的请求参数

在这里插入图片描述

–1,概述

简化了get方式参数的写法
普通的get传递的参数 http://localhost:8080/car/get?id=100&name=张三
restful传递的参数 http://localhost:8080/car/get2/100/张三

–2,测试

创建RunApp启动类
package cn.tedu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//位置:必须在所有资源之上的包里
@SpringBootApplication
public class RunApp {
   
    public static void main(String[] args) {
   
        SpringApplication.run(RunApp.class);
    }
}

创建CarController类
package cn.tedu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//@Controller
//@ResponseBody
@RestController
@RequestMapping("car")
public class CarController {
   
    //注意1:: 参数列表里的参数类型,最好使用引用类型,
    //如果浏览器没有传值过来就用默认值,但使用基本类型会抛异常的
    //解析普通的get传递的参数
    //http://localhost:8080/car/get?id=100&name=张三
    @RequestMapping("get")
//   public String get(int id,String name){
   
    public String get(Integer id,String name){
   
            return id+name ;
    }

    //解析restful传递的参数:简化了get方式参数的写法
    //http://localhost:8080/car/get2/100/张三
    @RequestMapping("get2/{id}/{name}")
 //{x}--通过{}获取访问路径中携带的参数,并且交给变量x保存
 //@PathVariable -- 获取{}中间变量的值
    public String get2(@PathVariable Integer id,
                       @PathVariable String name){
   
        return id+name;
    }
    //http://localhost:8080/car/get3/100/张三/red/9.9
    @RequestMapping("get3/{a}/{b}/{c}/{d}")
    public String get3(@PathVariable Integer a,
                       @PathVariable String b,
                       @PathVariable String c,
                       @PathVariable double d){
   
        return a+b+c+d ;
    }


}


创建前端网页文件
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		
<a href="http://localhost:8080/car/get?id=100&name=张三">解析get的参数 </a>

<a href="http://localhost:8080/car/get2/100/张三">解析restful风格的参数</a>
<a href="http://localhost:8080/car/get3/100/张三/red/9.9">练习解析restful风格的参数</a>
		
	</body>
</html>


测试

在这里插入图片描述
在这里插入图片描述

二,SpringMVC解析post的请求参数

–0,项目结构

在这里插入图片描述

–1,准备form表单

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值