0
votes

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;
}
}
1
What is your Driver object.mrtasln
driver is my object, I created on action definition. And When I debugged the receiving values at the beginning of function , Getting null value. Its not receving when using "@ModelAttribute Driver driver" code.Mr.DevEng
Can you show your Driver class.mrtasln
I added Driver class with question edited.Mr.DevEng

1 Answers

1
votes

Change your Driver entity like below :

@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;
}
}