This may be the result of an unspecified view, due to default view name generation错误解决

1、程序报错

javax.servlet.ServletException: Circular view path [employee]: would dispatch back to the current handler URL [/employee] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

2、项目背景

        (1)数据库字段设置了索引唯一

        (2)编写了一个异常处理器返回了自定义的数据类型

public class GlobalExceptionHandler {
    /**
     * SQLIntegrityConstraintViolationException的异常处理器
     * 新增数据库数据时,因某个字段是唯一的,当新增数据重复时,报SQLIntegrityConstraintViolationException
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(){
        return R.error("新增失败");
    }
}

        (3)还编写了一个过滤器默认拦截所有请求。

3、错误原因分析

        当我在页面上故意提交重复数据时,此时数据库底层会报SQLIntegrityConstraintViolationException异常,但是我编写了此异常的异常处理器,当程序报此异常时,就会进入到我编写的异常处理器的逻辑。

        客户端页面发送请求到服务器,我的异常处理器捕获到此异常后,会返回一个自定义类型的数据给客户端,然后被过滤器渲染视图并放行。

        但是却报了本文章主题This may be the result of an unspecified view, due to default view name generation的错误,翻译过来就是“这可能是由于默认视图名称生成而导致的未指定视图”。

        最终做到原因是我的异常处理器要向浏览器返回数据,但却没有加@ResponseBody注解,最后就可能导致过滤器渲染视图放行请求时出现错误。

4、错误解决

        (1)若程序使用了Thymeleaf模板引擎,出现此错误可能是因为缺少jar包,此时只需要在POM文件中引入即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

        (2)当服务器要向浏览器发送响应数据时,一定要检查代码是否标注@ResponseBody注解,若没有加入即可。

猜你喜欢

转载自blog.csdn.net/weixin_64709241/article/details/129238751