我正在尝试使用表单向我的数据库添加double类型的值 . 但是,当我尝试这样做时,我得到一个错误说明:
无法将[java.lang.String]类型的值转换为必需类型[com.javainuse.model.Grade];嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型[java.lang.String]转换为类型为“2.1”的类型[java.lang.Long];嵌套异常是java.lang.NumberFormatException:对于输入字符串:“2.1”
但是,如果我添加整数,则添加条目没有问题 . 任何人都可以确定我的错误在哪里?我看过类似的问题,但似乎没有任何效果:(我的代码如下:
Grade Controller:
@Controller
public class GradeController {
@Autowired
private GradeRepository gradeData;
@RequestMapping(value = "/addNewGrade.html", method = RequestMethod.POST)
public String newGrade(Grade grade) {
gradeData.save(grade);
return ("redirect:/listModules.html");
}
@RequestMapping(value = "/addNewGrade.html", method = RequestMethod.GET)
public ModelAndView addNewGrade() {
Grade grd = new Grade();
return new ModelAndView("newGrade", "form", grd);
}
Grade repository:
package com.javainuse.data;
import org.springframework.data.jpa.repository.JpaRepository;
import com.javainuse.model.Grade;
public interface GradeRepository extends JpaRepository {
}
Add New Grade form:
Grade
Module
${row.modulename}
Student
${row.firstname} ${row.lastname}
Grade Model:
@Entity
public class Grade {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private long id;
private int moduleid;
private int studentid;
private double grade;
public int getModuleid() {
return moduleid;
}
public void setModuleid(int moduleid) {
this.moduleid = moduleid;
}
public int getStudentid() {
return studentid;
}
public void setStudentid(int studentid) {
this.studentid = studentid;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
我的数据库中的数据类型也是双倍的 .