I am trying to insert 4 form fields name,age,location and address into database using spring data JPA repository method. When I am submitting my form into action , the controller not unable to receive the submitted values. Getting null in controller action. Here i trying using spring spring boot and spring data JPA. The following is my controller,
@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
}
And my form is ,
<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>
And In controller I am receiving the submitted form values using @ModelAttribute Like following,
@ModelAttribute Driver driver
I am troubleshooted by printing
driver.name;
driver.age;
driver.address;
driver.location;
But not accessing through this method. Is this receiving method is not correct? What I can do using this method? Otherwise is there any secondary method for achieving the form values using object type receiving???
My Driver Class is ,
package com.central.model;
import java.io.Serializable;
import 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;
}
}