Failed to convert value of type 'java.lang.String' to required type 'int' for proper问题解决办法

之前学习Spring-boot的时候,跟着csdn中的案例写了demo,结果报错了,就是以上的错误,现在把代码贴出来说下解决办法。

首先是Controller代码:

package com.Sheffy.demo.Controller;

import com.Sheffy.demo.Entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @RequestMapping(value = "/{id}")
    public String getUser(@PathVariable int id,Model model){
        model.addAttribute("user",new User(id,"ysh",13,"fj"));
        return "/user/detail";
    }

    @RequestMapping(value = "/listUser")
    public String listUser(Model model){
        List<User> listUser=new ArrayList<User>();
        for(int i=0;i<10;i++){

            listUser.add(new User(i,"ysh",18+i,"fj"));
        }
        model.addAttribute("users",listUser);
        return "/list";
    }

}

然后是代码结构:
结构图
html文件是放在templates的目录下的
properties文件中没有任何配置,之后运行,localhost:8080/user/listUser就报错了。
接下来说下解决办法,找了很久都没有找到,最后是同事帮忙解决的。
1、在properties文件中加入

# 模板配置
# 这个开发配置为false,避免改了模板还要重启服务器
spring.thymeleaf.cache=false
# 这个是配置模板路径的,默认就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/
# 这个可以不配置,检查模板位置
spring.thymeleaf.check-template-location=true
# 下面3个不做解释了,可以不配置
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

# 模板的模式
spring.thymeleaf.mode=HTML5

这个配置文件很奇怪,没加之前报错,加了运行正常之后,把配置文件注释掉还是运行正常。。
我的问题的解决办法就是这样。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值