Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法转换类型为';java.lang.String';至所需类型';int';对于不动产';id';_Java_Hibernate_Spring Boot_Jsp - Fatal编程技术网

无法转换类型为';java.lang.String';至所需类型';int';对于不动产';id';

无法转换类型为';java.lang.String';至所需类型';int';对于不动产';id';,java,hibernate,spring-boot,jsp,Java,Hibernate,Spring Boot,Jsp,我有一个非常简单的spring boot应用程序,试图将更新的位置保存到数据库中。但是,每当我检索保存的位置列表并将更新的位置保存到DB时,我都会遇到以下错误: 2019-11-27 09:29:05.330 WARN 14764 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springfr

我有一个非常简单的spring boot应用程序,试图将更新的位置保存到数据库中。但是,每当我检索保存的位置列表并将更新的位置保存到DB时,我都会遇到以下错误:

2019-11-27 09:29:05.330  WARN 14764 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'location' on field 'id': rejected value []; codes [typeMismatch.location.id,typeMismatch.id,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [location.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'id'; nested exception is java.lang.NumberFormatException: For input string: ""]]
以下是我的课程:

LocationRepository.java:

package com.springBoot.location.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.springBoot.location.entities.location;
import com.springBoot.location.service.LocationService;

@Controller
public class LocationController {

    @Autowired
    private LocationService service;

    @RequestMapping("/showCreate")
    public String showCreate() {
        return "createLocation";
    }

    //when the form in "createLocation.jsp" will be submitted, the same page "createLocation.jsp" will be returned, 
    @RequestMapping("/saveLoc")                                 //but this time, with a message, stating that the location has been saved.
    public String saveLocation(@ModelAttribute("location")location location,ModelMap modelmap) {//when the page will be saved, a "location" bean/object will be created
        location saveLocation = service.saveLocation(location);            //all the fields from that page will be saved into that object and
        String msg = "location saved with id "+saveLocation.getId();      //will be sent as request to the database
        modelmap.addAttribute("msg",msg);//modelMap is for handling responses from the db.
        return "createLocation";                                           
    } 

    @RequestMapping("/displayLocations")
    public String displayLocations(ModelMap modelMap) {
        List<location> allLocations = service.getAllLocations();
        modelMap.addAttribute("locations",allLocations);
        return "displayLocations";
    }

    @RequestMapping("/addLoc")
    public String showCreate1() {
        return "createLocation";
    }

    @RequestMapping("/getID")
    public String getId(@ModelAttribute("location")location location,ModelMap modelMap) {
        List<location> allLocations = service.getAllLocations();
        location Lastlocation = allLocations.get(allLocations.size()-1);
        int id = Lastlocation.getId();
        String lastId = "The last ID was "+id;
        modelMap.addAttribute("lastId",lastId);
        return "createLocation";

    }

    @RequestMapping("/deleteLocation")
    public String deleteLocation(@RequestParam("id")int id,ModelMap modelMap) {
        location l = service.getLocationById(id);
        service.deleteLocation(l);
        List<location> allLocations = service.getAllLocations();
        modelMap.addAttribute("locations",allLocations);
        return "displayLocations";
    }

    @RequestMapping("/updateLocation")
    public String updateLocation(@RequestParam("id")int id,ModelMap modelMap) { 
        location l = service.getLocationById(id);
        modelMap.addAttribute("locations",l);
        return "updateLocation";

    }

    @RequestMapping("/updateLoc")
    public String saveUpdateLocation(@ModelAttribute("location")location location,ModelMap modelMap) {
        service.updateLocation(location);
        List<location> locations = service.getAllLocations();
        modelMap.addAttribute("locations",locations);
        return "displayLocations";
    }




}
下面是我的jsp文件:

createLocation.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create location</title>
</head>
<body>
 <form action="saveLoc" method="post"> <!-- value of the "action" attribute is same as the value inside "request controller" method name, to which the method maps to --> 
 <pre>
    Id:<input type="text" name="id"/><!-- the value in the "name" attribute should be same as the variable names in the "entity" class -->
    <a href="getID">Want to know the last ID ?</a>
    Code:<input type="text" name="code"/>
    Name:<input type="text" name="name"/>
    Type: Urban<input type="radio" name="type" value="URBAN"/><!-- when the page will be saved, the value will be saved as "URBAN" -->
          Rural<input type="radio" name="type" value="RURAL"/>
     <input type="submit" value="save"/>
 </pre>
 </form>
${lastId}<br>
${msg}<br>
 <a href="displayLocations">View all</a> <!-- the value in the "href" attribute will be inside the "RequestMapping" 
</body>                                                     annotation mapping to a specific class for handling a request -->
</html>
Id: <input type="number" name="id" value="${location.id}" readonly/>

创建位置
身份证件:
代码:
姓名:
类型:市区
乡村的
${lastId}
${msg}
displayLocations.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create Location</title>
</head>
<body>

<form action="updateLoc" method="post">
<pre>
Id: <input type="text" name="id" value="${location.id}" readonly/>
Code: <input type="text" name="code" value="${location.code}"/>
Name: <input type="text" name="name" value="${location.name}"/>
Type: Urban <input type="radio" name="type" value="URBAN" ${location.type=='URBAN'?'checked':''}/>
    Rural <input type="radio" name="type" value="RURAL" ${location.type=='RURAL'?'checked':''}/>
Email: <input type="text" name="email" value="${location.name}"/>
Phone: <input type="text" name="phone" value="${location.name}"/>
Address: <input type="text" name="address" value="${location.name}"/>
<input type="submit" value="save"/>
</pre>
</form>

</body>
</html>

地点清单
身份证件
代码
名称
类型
${location.id}
${location.code}
${location.name}
${location.type}

updateLocation.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create location</title>
</head>
<body>
 <form action="saveLoc" method="post"> <!-- value of the "action" attribute is same as the value inside "request controller" method name, to which the method maps to --> 
 <pre>
    Id:<input type="text" name="id"/><!-- the value in the "name" attribute should be same as the variable names in the "entity" class -->
    <a href="getID">Want to know the last ID ?</a>
    Code:<input type="text" name="code"/>
    Name:<input type="text" name="name"/>
    Type: Urban<input type="radio" name="type" value="URBAN"/><!-- when the page will be saved, the value will be saved as "URBAN" -->
          Rural<input type="radio" name="type" value="RURAL"/>
     <input type="submit" value="save"/>
 </pre>
 </form>
${lastId}<br>
${msg}<br>
 <a href="displayLocations">View all</a> <!-- the value in the "href" attribute will be inside the "RequestMapping" 
</body>                                                     annotation mapping to a specific class for handling a request -->
</html>
Id: <input type="number" name="id" value="${location.id}" readonly/>

创建位置
身份证件:
代码:
姓名:
类型:市区
乡村的
电邮:
电话:
地址:

将ID属性的输入类型更改为数字,如下所示

Id:

检查您的位置表的id是否为int字段而不是char/varchar。@RahulAgrawal它是int看起来像
id
正在作为
从客户端JSP传递到控制器,然后再传递到DB。如果
id
是一个自动递增的字段,那么您需要在Hibernate中相应地处理该字段,并且不要尝试将该值插入
id
列,因为它不是自动递增的。