一、springboot入门程序撰写并启动
网址1:https://start.aliyun.com/
网址2:https://start.spring.io/
启动类
package edu.wust;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类 -- 启动springboot工程
@SpringBootApplication
public class SpringbootWeb01Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootWeb01Application.class, args);
}
}
请求处理类
package edu.wust.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//请求处理类
@RestController
public class HelloController {
@RequestMapping("/myhello")
public String myhello(){
System.out.println("Hello World");
return "Hello World";
}
}
启动界面
二、使用postman练习参数的获取
1、postman工具
2、请求--简单参数
原始方式
RequestController
package edu.wust.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* 测试请求参数接收
*/
@RestController
public class RequestController {
//原始方式
@RequestMapping("/simpleParam")
public String simpleParam(HttpServletRequest request){
//获取请求参数
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
if (ageStr == null){ //由于不考虑age为null值时进行数值转换总是报错,于是采用条件判断解决
return"info = null";
}else {
int age = Integer.parseInt(ageStr);
System.out.println(name+":"+age);
return "OK";
}
}
}
浏览器运行
postman--请求参数
springboot方式
RequestController
package edu.wust.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* 测试请求参数接收
*/
@RestController
public class RequestController {
//springboot方式
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
System.out.println(name+":"+age);
return "OK";
}
}
浏览器运行
postman--get请求参数
postman--post请求参数
(接受的是null的需要把请求方式改为post)
3、请求--实体参数
若简单参数过多,可将所有参数封装至一个实体类当中
简单实体参数的封装
(请求参数名与形参对象属性名相同)
user-->name + age
User
package edu.wust.pojo;
public class User {
private String name;
private Integer age;
//alt+insert快捷键生成对应的Getter and Setter以及toString()方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
RequestController
package edu.wust.controller;
import edu.wust.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* 测试请求参数接收
*/
@RestController
public class RequestController {
//实体参数
@RequestMapping("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
return "OK";
}
}
发送请求
复杂实体参数的封装
user --> name+age+address
address --> province+city
User(添加了Address类以及三种方法)
package edu.wust.pojo;
public class User {
private String name;
private Integer age;
private Address address;
//alt+insert快捷键生成对应的Getter and Setter以及toString()方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
Address
package edu.wust.pojo;
public class Address {
private String province;
private String city;
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
RequestController
发送请求
4、请求--数组集合参数
数组封装
请求参数名与形参中数组变量名相同,可以直接使用数组封装
接收函数
发送请求
控制台
集合封装
请求参数名与形参中集合变量名相同,集合前使用@RequestParam来绑定参数关系
(List<>有序集合)
接收函数
发送请求
控制台
5、请求-日期参数
使用@DateTimeFormat注解完成日期参数格式转换(pattern属性,指定日期格式,例如:pattern="yyyy-MM-dd HH:mm:ss")
接收函数
发送请求
控制台
6、请求-json参数(请求方式须post-boy-raw-JSON)
在postman发送请求时如何传递json参数
在服务端Controller中怎么接受json格式参数
三、体会前端页面向后端发送数据的过程,并且自己尝试将之前的注册页面的信息发送到服务端
(一)product.html的操作代码,输入产品名称和价格,点击“增加商品”按钮,页面返回“ok”。服务台返回用户输入的值。
ProductController
package edu.wust.controller;
import edu.wust.pojo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import edu.wust.pojo.Product;
import javax.servlet.http.HttpServletRequest;
@RestController
public class ProductController {
@RequestMapping("/addProduct1")
public String simpleParam1(HttpServletRequest request) {
String name = request.getParameter("name");
String ageStr = request.getParameter("price");
int price = Integer.parseInt(ageStr);
System.out.println("addProduct1:" + name + " : " + price);
return "OK";
}
@RequestMapping("/addProduct")
public String simpleParam(String name, Integer price) {
System.out.println("您输入的信息是:" + name + " : " + price);
return "OK";
}
@RequestMapping("/addProduct2")
public String simpleParam2(Product product) {
System.out.println(product);
return "OK";
}
}
pojo.Product
package edu.wust.pojo;
public class Product {
private String name;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
product.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>产品详情</title>
</head>
<body style="font-size: 30px">
<form action="addProduct">
产品名称 :<input type="text" name="name" value=""><br />
产品价格: <input type="text" name="price" value=""><br />
<input type="submit" value="增加商品">
<br><br><br><br>test2,addProduct1 ,productResult.html
</form>
</body>
</html>
运行