Java 无法将类型字符串转换为类型long Spring Boot
我正在尝试使用表单向数据库中添加一个double类型的值。但是,当我尝试执行此操作时,会出现一个错误,说明: 无法将[java.lang.String]类型的值转换为所需类型 [com.javainuse.model.Grade];嵌套异常是 org.springframework.core.convert.ConversionFailedException:未能 将[java.lang.String]类型转换为[java.lang.Long]类型 价值"2.1";;嵌套异常为java.lang.NumberFormatException:For 输入字符串:“2.1” 但是,如果我添加一个整数,则添加条目时不会出现问题。有人能指出我的错误在哪里吗?我看过类似的问题,但似乎没有任何效果:(我的代码如下: 等级管理员:Java 无法将类型字符串转换为类型long Spring Boot,java,spring,jsp,spring-boot,h2,Java,Spring,Jsp,Spring Boot,H2,我正在尝试使用表单向数据库中添加一个double类型的值。但是,当我尝试执行此操作时,会出现一个错误,说明: 无法将[java.lang.String]类型的值转换为所需类型 [com.javainuse.model.Grade];嵌套异常是 org.springframework.core.convert.ConversionFailedException:未能 将[java.lang.String]类型转换为[java.lang.Long]类型 价值"2.1";;嵌套异常为java.lang
@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);
}
package com.javainuse.data;
import org.springframework.data.jpa.repository.JpaRepository;
import com.javainuse.model.Grade;
public interface GradeRepository extends JpaRepository<Grade, Long> {
}
<form name="form" action="" method="POST">
<div>
<label>Grade </label>
<input name="grade" />
</div>
<div>
<label>Module</label>
<select name="moduleid">
<c:forEach items="${module.rows}" var="row">
<option value="${row.id}">${row.modulename}</option>
</c:forEach>
</select>
</div>
<div>
<label>Student</label>
<select name="studentid">
<c:forEach items="${student.rows}" var="row">
<option value="${row.id}">${row.firstname} ${row.lastname}</option>
</c:forEach>
</select>
</div>
<br>
<input type="submit" value="Submit"/>
</form>
@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;
}
}
等级存储库:
@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);
}
package com.javainuse.data;
import org.springframework.data.jpa.repository.JpaRepository;
import com.javainuse.model.Grade;
public interface GradeRepository extends JpaRepository<Grade, Long> {
}
<form name="form" action="" method="POST">
<div>
<label>Grade </label>
<input name="grade" />
</div>
<div>
<label>Module</label>
<select name="moduleid">
<c:forEach items="${module.rows}" var="row">
<option value="${row.id}">${row.modulename}</option>
</c:forEach>
</select>
</div>
<div>
<label>Student</label>
<select name="studentid">
<c:forEach items="${student.rows}" var="row">
<option value="${row.id}">${row.firstname} ${row.lastname}</option>
</c:forEach>
</select>
</div>
<br>
<input type="submit" value="Submit"/>
</form>
@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;
}
}
我的数据库中的数据类型也是双重的。它尝试将表单数据中传递的
等级绑定到控制器方法参数:
您可以:
- 向控制器方法添加
@modeldattribute
注释,如:
- 将
grade
类中grade
字段的名称更改为其他名称(例如value
)
值“2.1”是无效的长值。
您可以检查对应数据类型的最大值和最小值,如下所示:
String s="2.1";
System.out.println(Long.MAX_VALUE);//9223372036854775807
System.out.println(Double.MAX_VALUE);//1.7976931348623157E308
因此,要么更改实体对应属性的数据类型(使用Double或String),要么进行前端验证以限制输入类型。Hi,grade设置为Double(我唯一的长值是id,它没有添加到表单中)你能在github上发布完整的项目吗?嗨@Maciej,我不熟悉github,因此不知道如何使用它。我是spring boot的初学者,因此发布的代码是我唯一与年级模型相关的代码。好的,将grade
内grade
类的字段名称更改为value
-它可以工作hen as expected.hi不起作用-grade类中的字段grade是我的表列可能失败的原因是,值2.1正试图放入您的grade的id变量中…您确定在尝试解析双级变量而不是长id变量时引发了特定的异常吗?@RAZ_Muh_Taz hi谢谢你的回复。我想可能是这样,但是当我添加一个没有小数点的数字时,它被添加到数据库中,没有错误,所以它不能是那样。所以你使用一个大的明显的数字,比如9999,这个值被放入数据库中,是分数的值而不是id?你确定吗?是的-尝试了多次es,检查了数据库,在本例中输入的是grade。好的,很好,您100%确定数据库的grade列的类型是double,而不是Long??抱歉,我不知道框架,但从错误中可以清楚地看出,您试图将字符串解析为您的程序/数据库认为应该的类型d是一个长的而不是你想要的双精度I,数据类型是双精度的-这是我的问题。我不知道为什么当我实际输入“双精度”值类型时,错误显示字符串转换为长的错误。你能告诉我绑定的属性吗?即,值绑定到哪个属性??