1、springboot入门程序撰写并启动。
创建springboot工程
2、使用postman练习参数的获取。
请求工具postman下载,运行第一个入门程序
1.获取参数
1.简单参数的接受
1.运用原始的方法获取参数
public String simpleParam(HttpServletRequest request){
//获取请求参数
String name=request.getParameter("name");
String ageStr= request.getParameter("age");
int age= Integer.parseInt(ageStr);
System.out.print(name+";"+age);
return "OK";
2.运用springboot方式获取参数
3.@RequestParam注解
2.实体参数的接收
简单实体对象的封装
java类中的代码:
package com.itheying.pojo;
import org.springframework.web.bind.annotation.RequestMapping;
public class User {
private String name;
private Integer age;
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 +
'}';
}
}
复杂实体对象的封装
address的封装
3.数组集合参数
数组参数
集合参数
4.日期请求参数
5.JSON参数
json数据的健必须和user类中的名字相同
6.路径请求参数
传递一个路径参数
path后面是不固定的路径参数
传递多个路径参数
3、体会前端页面向后端发送数据的过程。并且自己尝试将之前的注册页面的信息发送到服务端。
(1)、product.html的操作代码,输入产品名称和价格,点击“增加商品”按钮,页面返回“ok”。服务台返回用户输入的值。相关页面如下:
ProductController.java
package com.itheying.controller;
import com.itheying.pojo.Product;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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";
}
}
product.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>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>
product.java
package com.itheying.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;
}
}
(2)、自己尝试将之前的注册页面的信息发送到服务端。
biaodan.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单</title>
</head>
<body>
<form action="form">
<ul style="list-style: none; line-height: 30px">
<li>
输入用户姓名:
<input type="text" name="name" />
<br />
</li>
<li>
选择性别:
<input name="sex" type="radio" value="boy" />
男
<input name="sex" type="radio" value="girl" />
女
</li>
<li>
选择密码提示问题:
<select name="question">
<option value="母亲生日">
母亲生日
</option>
<option value="宠物名称">
宠物名称
</option>
<option value="电脑配置">
电脑配置
</option>
</select>
</li>
<li>
请输入问题答案:
<input type="text" name="key" />
</li>
<li>
请选择个人爱好:
<div style="width: 400px">
<input name="like" type="checkbox" value="dance" />
唱歌跳舞
<input name="like" type="checkbox" value="web" />
上网冲浪
<input name="like" type="checkbox" value="hill" />
户外登山
<br />
<input name="like" type="checkbox" value="sports" />
体育运动
<input name="like" type="checkbox" value="reading" />
读书看报
<input name="like" type="checkbox" value="movie" />
欣赏电影
</div>
</li>
<li>
<input type="submit" value="提交" />
</li>
</ul>
</form>
</body>
</html>
biaoController.java
package com.itheying.controller;
import com.itheying.pojo.Biao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class biaoController {
@RequestMapping("/form")
public String simpleParam2( Biao biao){
System.out.println(biao);
return "OK";
}
}
Biao.java
package com.itheying.pojo;
import java.util.Arrays;
public class Biao {
private String name;
private String sex;
private String key;
private String[] like;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String[] getLike() {
return like;
}
public void setLike(String[] like) {
this.like = like;
}
@Override
public String toString() {
return "Biao{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", key='" + key + '\'' +
", like=" + Arrays.toString(like) +
'}';
}
}