Spring mvc 提交表单时,表单值未进入控制器
我试图使用SpringDataJPA存储库方法将4个表单字段名称、年龄、位置和地址插入数据库。当我将表单提交到操作中时,控制器不能接收提交的值。在控制器操作中获取null。这里我尝试使用spring引导和spring数据JPA。下面是我的控制器Spring mvc 提交表单时,表单值未进入控制器,spring-mvc,spring-data-jpa,Spring Mvc,Spring Data Jpa,我试图使用SpringDataJPA存储库方法将4个表单字段名称、年龄、位置和地址插入数据库。当我将表单提交到操作中时,控制器不能接收提交的值。在控制器操作中获取null。这里我尝试使用spring引导和spring数据JPA。下面是我的控制器 @RequestMapping(value="/driverSubmit", method=RequestMethod.POST) public ModelAndView driverSubmit(@ModelAttribute Driver drive
@RequestMapping(value="/driverSubmit", method=RequestMethod.POST)
public ModelAndView driverSubmit(@ModelAttribute Driver driver, Model model)
{
model.addAttribute("driver", driver);
driverRepo.save(driver);
return new ModelAndView("redirect:/dHome"); //to home page insertoin
}
我的表格是
<form action="driverSubmit" method="post" >
<table width="300px" height="200px" align="center">
<tr>
<td>
<b> Name </b>
</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>
<b> Age</b>
</td>
<td>
<input type="text" name="age"/>
</td>
</tr>
<tr>
<td>
<b> Address</b>
</td>
<td>
<input type="text" name="address"/>
</td>
</tr>
<tr>
<td>
<b> Location</b>
</td>
<td>
<input type="text" name="location" />
</td>
</tr>
</table>
<div>
<input type="submit" value="Submit"/>
</div>
</form>
我被印刷所困扰
driver.name;
driver.age;
driver.address;
driver.location;
但不是通过这种方法访问。这种接收方式是否不正确?使用这种方法我能做什么?否则,是否有使用对象类型接收实现表单值的第二种方法
我的驾驶课是
包com.central.model
导入java.io.Serializable
导入javax.persistence.*
@Entity
@Table(name = "drivers")
public class Driver implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer id;
@Column(name = "name")
public String name;
@Column(name = "age")
public Integer age;
@Column(name = "address")
public String address;
@Column(name = "location")
public String location;
public Driver() {
}
public Driver(String name, Integer age, String address , String location ) {
this.name = name;
this.age = age;
this.address = address;
this.location = location;
}
}
更改驱动程序实体,如下所示:
@Entity
@Table(name = "drivers")
public class Driver implements Serializable {
private Integer id;
private String name;
private Integer age;
private String address;
private String location;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "location")
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Driver() {
}
public Driver(String name, Integer age, String address , String location ) {
this.name = name;
this.age = age;
this.address = address;
this.location = location;
}
}
什么是你的驱动程序对象。驱动程序是我的对象,我根据动作定义创建的。当我调试函数开头的接收值时,得到的是null值。当使用“@modeldattribute Driver-Driver”代码时,它没有接收到。您能显示您的驱动程序类吗?我添加了驱动程序类,并编辑了问题。此新更改正在运行。谢谢您的回复,先生。很高兴能为您提供帮助
@Entity
@Table(name = "drivers")
public class Driver implements Serializable {
private Integer id;
private String name;
private Integer age;
private String address;
private String location;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "location")
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Driver() {
}
public Driver(String name, Integer age, String address , String location ) {
this.name = name;
this.age = age;
this.address = address;
this.location = location;
}
}