首页 文章

无法将类型字符串转换为类型long Spring Boot

提问于
浏览
2

我正在尝试使用表单向我的数据库添加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<Grade, Long> {

}

Add New Grade form:

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

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

}

我的数据库中的数据类型也是双倍的 .

2 回答

  • 1

    它尝试将 grade 内部传递的表单数据绑定到控制器方法参数:

    你可以:

    • @ModelAttribute 注释添加到控制器方法中,如:
    @RequestMapping(value = "/addNewGrade.html", method = RequestMethod.POST)
    public String newGrade(@ModelAttribute("form") Grade grade) {
    
        gradeData.save(grade);
        return "redirect:/listModules.html";
    
    }
    
    • Grade 类中 grade 字段的名称更改为不同的名称(例如 value ) .
  • 0

    值“2.1”是无效的长值 . 您可以检查相应数据类型的最大值和最小值,如下所示:

    String s="2.1";     
    System.out.println(Long.MAX_VALUE);//9223372036854775807
    System.out.println(Double.MAX_VALUE);//1.7976931348623157E308
    

    因此,要么更改实体的相应属性的数据类型(使用Double或String),要么执行前端验证以限制输入类型 .

相关问题