springboot___addViewController配置不生效的问题及原因
问题:
1)前提
springboot项目通过实现WebMvcConfigurer并重写addViewControllers方法添加视图控制器:

registry.addViewController("/admin/index.html").setViewName("admin/index");
registry.addViewController("/student/index-stu.html").setViewName("student/index-stu");
登录判断密码正确后重定向首页:

2)测试报错
用户成功登录添加到session中,但是页面跳转失败:

报错内容:
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "index.html"
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "index.html"
但是自己在controller里写一个请求映射就可以访问,这个是为什么呢?
@RequestMapping("/admin/index.html")
public String toAdminIndex(){
return "admin/index";
}
@RequestMapping("/student/index-stu.html")
public String toStudentIndex(){
return "student/index-stu";
}
原因
thymeleaf的版本和springboot版本有冲突
我的pom.xml:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
把springboot版本换成2.2.5.RELEASE应该能行
参考链接: https://blog.csdn.net/weixin_40118894/article/details/104567518 https://blog.csdn.net/weixin_43849277/article/details/107867736
浙公网安备 33010602011771号