提交表单时,表单值不会进入控制器

时间:2017-10-03 06:17:45

标签: spring-mvc spring-data-jpa

我正在尝试使用spring数据JPA存储库方法将4个表单字段名称,年龄,位置和地址插入到数据库中。当我提交表单时,控制器无法接收提交的值。在控制器操作中获取null。在这里,我尝试使用弹簧弹簧和弹簧数据JPA。以下是我的控制器,

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

在控制器中,我使用@ModelAttribute接收提交的表单值,如下所示,

@ModelAttribute Driver driver

我通过打印进行故障排除

driver.name;
driver.age;
driver.address;
driver.location;

但不能通过此方法访问。这种接收方法不正确吗?我用这种方法可以做什么?否则是否有任何使用对象类型接收实现表单值的辅助方法???

我的驱动程序类是,

package com.central.model;

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

1 个答案:

答案 0 :(得分:1)

更改您的驱动程序实体,如下所示:

{{1}}