解析请求参数方式:③
1.get
参数列表的写法:
1.参数类型必须与请求的参数类型一致
2.参数名称必须与请求的参数名称一致
HandlerMapping 作用是根据地址栏来找到类与方法
底层是Map结构 Map<String,Object> {"/car/save","new CarController().save()"}
SpringMVC框架的作用:
1.接收了请求,顺便解析请求数据(在方法的参数列表中声明即可)
/*避免400(参数类型不匹配) 500(后端抛出异常)异常:用包装类代替基本类型
/*参数列表过长,可以提供一个对象,可以把参数设置成对象的属性值,直接给对象属性赋值
2.把解析到的参数调用c.setXxx()设置值
数据传入数据库
执行顺序:
看到网页→访问超链接(H5内a标签)→处理器映射器找到方法→处理器适配器调用方法→get数据→框架帮助获取值(从地址栏取)
@RequestMapping("save2")
public Car save2(Car car) throws Exception {
jdbc(car);
return car;
}
public void jdbc(Car car) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/aaa?characterencoding=utf8";
Connection c = DriverManager.getConnection(url, "root", "root");
String sql="insert into car values(null,?,?,?,?)";
PreparedStatement s = c.prepareStatement(sql);
s.setString(1,car.getBrand());
s.setString(2,car.getType_car());
s.setString(3,car.getColor());
s.setDouble(4,car.getPrice());
int i = s.executeUpdate();
System.out.println(i);
s.close();
c.close();
}
2.restful
@RequestMapping("insert2/{id}/{name}/{age}")
public void insert2(@PathVariable Integer id,
@PathVariable String name,
@PathVariable Integer age){
System.out.println(use);
}
}//http://localhost:8080/user/insert2/1/张三/118
3.post
form表单提交数据要求:1.form 2.submit 3.配置name属性
action 指定跳转网址 method 指定提交方式
项目结构
pojo???? 一个类 全是属性+get()+set()
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生信息管理系统</title>
<script src="day11/vue.js">
</script>
<style>
#id{
background-color: #A52A2A;
}
#re{
background-color: #9370DB;
}
</style>
</head>
<body>
<form action="http://localhost:8080/student/save" method="post" >
<h2>学生信息管理系统MIS</h2>
<table>
<tr>
<td>姓名:<br>
<input type="text" name="name" placeholder="请输入姓名..." class="a">
</td>
</tr>
<tr>
<td>年龄:<br>
<input type="number" name="age" placeholder="请输入年龄..." class="a">
</td>
</tr>
<tr>
<td>
性别:(单选框)
<input type="radio" name="sex" value="0" checked="checked"/>男
<input type="radio" name="sex" value="1" />女
</td>
</tr>
<tr>
<td>
爱好:(多选)
<input type="checkbox" name="hobby" checked="checked" value="ppq"/>乒乓球
<input type="checkbox" name="hobby" value="ps"/>爬山
<input type="checkbox" name="hobby" value="cg"/>唱歌
</td>
</tr>
<tr>
<td>
学历:(下拉框)
<select name="edu">
<option value ="1">本科</option>
<option value ="2">专科</option>
<option value ="3">研究生</option>
</select>
</td>
</tr>
<tr>
<td>
入学日期:<br>
<input type="date" name="intime">
</td>
</tr>
<tr>
<td>
<input type="submit" name="sb" id="sb" onclick="fun();">
<input type="reset" name="re" id="re">
</td>
</tr>
</table>
</form>
</body>
</html>
controller
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("student")
public class StudentController {
@RequestMapping("save")
public String save(Student stu){
return "保存成功";
}
}
pojo
package cn.tedu.pojo;
//充当了mvc的m层,封装数据
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Arrays;
import java.util.Date;
public class Student {
private String name;
private Integer age;
private Integer sex;
private String[] hobby;
private Integer edu;
// 400异常 页面输入默认是String类型 String→Date
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date intime;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
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 Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Integer getEdu() {
return edu;
}
public void setEdu(Integer edu) {
this.edu = edu;
}
public Date getIntime() {
return intime;
}
public void setIntime(Date intime) {
this.intime = intime;
}
}
submit→action→类→方法→框架帮忙自动封装属性值
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_55740233/article/details/120292501
内容来源于网络,如有侵权,请联系作者删除!