【问题标题】:Failed to convert value of type 'java.lang.String' to required type 'int';无法将“java.lang.String”类型的值转换为所需的“int”类型;
【发布时间】:2019-11-05 08:24:07
【问题描述】:

将 id 值传递给控制器​​会出现错误“无法将类型 'java.lang.String' 的值转换为所需的类型 'int'”。

但是当我打印 System.out.println(studentService.findById(id)) 时,它会在终端中输出为 可选[学生{id=1, firstName='Abhishek', lastName='Shukla', branch='CS', year=4, mobileNumber='9876543210'}]

我已经尝试将 id 作为字符串传递,并通过在控制器中使用 Integr.parseInt(id) 将字符串转换为 int,但它会引发错误 “ servlet [dispatcherServlet] 的 Servlet.service() 在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“{id}”],根本原因是“


    <h1>Student Place</h1>
    <form name="myForm" action="/student/{id}" >
        <table border="1">
            <tbody>
                <tr>
                    <td>Student ID :</td>
                    <td><input type="text" name="id" size="10" /> </td>
                </tr>
            </tbody>
         </table>
         <br>
        <input type="submit" value="submit" name="submit" /><br>
        <input type="reset" value="Rest" name="reset" />
     </form>


@Controller
public class StudentController {

    @Autowired
    private StudentService studentService  ;

    @GetMapping(value = "/students")
    public  String index(ModelMap modelMap){
        modelMap.put("students" , studentService.findAll());
        return "index" ;
    }

    @GetMapping(value = "/student/{id}")
    public String data(@PathVariable("id") int id , ModelMap modelMap){
        modelMap.put("students" , studentService.findById(id)) ;
        System.out.println(studentService.findById(id));

        return "studentbyid" ;
    }

    @GetMapping(value = "/student")
    public String databyid(){
        return "student" ;
    }
}



public interface StudentService {

    public Iterable<Student> findAll() ;
    public Optional<Student> findById(int id) ;
}


@Transactional
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository ;

    @Override
    public Iterable<Student> findAll() {
        return studentRepository.findAll();
    }

    public Optional<Student> findById(int id) {
        return studentRepository.findById(id);
    }
}


@Entity
@Table(name = "student")
public class Student {

    @Id
    @Column(name="id")
    private int id ;

    @Column(name="first_name")
    private String firstName ;

    @Column(name="last_name")
    private String lastName ;

    @Column(name="branch")
    private String branch ;

    @Column(name="year")
    private int  year ;

    @Column(name="mobile_number")
    private String mobileNumber ;

    public Student() {
    }

    public Student(int id, String firstName, String lastName, String branch, int year, String mobileNumber) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.branch = branch;
        this.year = year;
        this.mobileNumber = mobileNumber;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", branch='" + branch + '\'' +
                ", year=" + year +
                ", mobileNumber='" + mobileNumber + '\'' +
                '}';
    }
}

白标错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。

出现意外错误(类型=错误请求,状态=400)。 无法将“java.lang.String”类型的值转换为所需的“int”类型;嵌套异常是 java.lang.NumberFormatException: For input string: "{id}"

【问题讨论】:

    标签: spring-boot spring-mvc jsp


    【解决方案1】:

    表单的动作是

    action="/student/{id}"
    

    因此,如错误消息所示,您实际上是在向/student/{id} 发送请求,而不是/student/1234

    显然,表单有一个输入字段,用户必须在其中输入 ID,ID 将作为请求参数发送,而不是作为路径变量发送,因此控制器方法和操作的路径应该是

    /students
    

    它应该将ID作为请求参数:

    @GetMapping(value = "/student")
    public String data(@RequestParam("id") int id, ModelMap modelMap) 
    

    【讨论】:

    • 如何将 jsp 表单中的值作为@PathVariable 传递
    • 在您的情况下,您需要使用 JavaScript。
    【解决方案2】:

    请检查属性文件并确保您没有在指定属性后放置分号(;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2020-07-02
      • 2017-01-28
      • 2019-08-23
      • 2020-09-07
      • 1970-01-01
      • 2021-06-13
      • 2015-01-22
      相关资源
      最近更新 更多