类型不匹配异常:无法将字符串(java.string)转换为所需类型(java.lang.integer)

wvmv3b1j  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(492)

我一直试图在我的申请表提交,但我不断得到以下错误 2021-07-05 21:40:38.910[0;39m [33m WARN[0;39m [35m18360[0;39m [2m---[0;39m [2m[nio-8080-exec-3][0;39m [36m.w.s.m.s.DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.issuetracking.app.enitities.Equipment'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'lights'; nested exception is java.lang.NumberFormatException: For input string: "lights"] 我已经这样写了我的控制器。它由显示使用表单输入的数据列表的处理程序方法组成。在表单中添加设备详细信息的处理程序方法。但是,我一直在尝试在同一页上执行此操作,以便在表单中提交数据时,数据应显示在同一页表单下方的表格中。我哪里出了问题,我如何才能做到这一点?提前谢谢。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.issuetracking.app.enitities.Equipment;
import com.issuetracking.app.services.EquipmentService;

@Controller
public class EquipmentController {

    private EquipmentService equipmentService;

    public EquipmentController(EquipmentService equipmentService) {
        super();
        this.equipmentService = equipmentService;
    } 

    @GetMapping("/equipment")
    public String listEquipment(Model model) {
        model.addAttribute("equipment",equipmentService.getAllEquipment()); 
        return "equipment";
    }

    @GetMapping("/equipment/new")
    public String addEquipment(Model model) {
         Equipment equipment = new Equipment();
        model.addAttribute("equipment", equipment); 
        return "equipment"; 
    }

    @PostMapping("/equipment")
    public String saveEquipment(@ModelAttribute("equipment") Equipment equipments) {
    System.out.println(equipmentService.saveEquipment(equipments));
    return "redirect:/equipment";
    }

}
ukxgm1gy

ukxgm1gy1#

基于这个错误,我会说有一个属性为“filled”的字符串值“lights”。在Map过程中,这些值不能转换为整数,因为对应的属性需要一个整数。我想说,输入对象是不正确的。

相关问题