【问题标题】:Circular View Path on trying to return view from Controller in Springboot app尝试从 Spring Boot 应用程序中的控制器返回视图的循环视图路径
【发布时间】:2017-03-29 12:03:40
【问题描述】:

这是一个在 Spring 网站上在线下载的 Spring Initialzr 项目。我正在使用普通的@Controller 并返回一个字符串,它应该返回视图名称。我在位置src/main/resources/templates/ 中有dog.html。 控制器和 html 文件发布在下面。

下面是dog.html

<!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
     <title>Getting Started: Serving Web Content</title>  
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />` 
     </head>
    <body>
    <p> ggg</p>
     <p th:text="'Hello, ' + ${dogBreed.breedName} + '!'" />
    </body>
    </html>

ViewController 下面

@Controller
@RequestMapping("/Disney")
public class ViewController {

    @Autowired
    BreedService breedService;

    @RequestMapping(value = "/{breedName}")
    public String getDogsbyBreedName(@PathVariable("breedName") String breedName, Model model) {

        //call a method which returns the Object Breed.

        // store this returned Object "breed" and return it as an html view to display as Graphical content.
        BreedMaster breed = breedService.getAllDogsByBreedName(breedName);
        model.addAttribute("dogBreed",breed);
        return "dog";
    }

    @RequestMapping(value = "/viewtest")
    public String sampleTest(){


        return "sample";
    }

}

下面的sample.html

 <!DOCTYPE HTML>
    <html>
    <head>
     <title>Getting Started: Serving Web Content</title>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p> sample html</p>
    </body>
    </html>

breedService.getAllDogsByBreedName(breedName) 方法按品种名称返回狗列表,我想将其发送到 HTML dog.html。但请注意,即使对于 url /viewtest,它也不会重新发送 sample.html 抛出错误。这意味着我无法达到dog.htmlsample.html 并且从未更改过任何springboot 自动配置。非常需要帮助,我认为这是一个愚蠢的错误,请任何人指出,谢谢。

我在调用/viewtest 时收到的错误消息如下:

我在调用/{breedName} 时收到的错误消息如下: 请注意,重新输入的字符串“dog”与输入的“/{breedName}”不同,后者是“Labrador”

【问题讨论】:

    标签: java html spring-mvc spring-boot thymeleaf


    【解决方案1】:

    我终于找到了问题,并找到了解决方案。 我正在使用 Thymeleaf 模板引擎,所以在我的 dog.html 中使用了它,但添加了

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

    版本号为 3.0.1,但后来将版本号更改为 1.2.1.RELEASE 有效。在此之后,我可以从 @Controller 类方法返回所有 html 文件。不知道它如何影响即使是简单的 html,也不知道它为什么会抛出 Circular view path 错误。

    【讨论】:

      【解决方案2】:

      尝试在spring boot中取消选中spring-security并重新检查数据库配置(application.properties中的属性,配置类)。

      【讨论】:

      • spring.h2.console.enabled: true logging.level.org.hibernate.SQL: debug spring.datasource.url: jdbc:h2:~/DisneyDB;AUTO_SERVER=TRUE spring.jpa.hibernate .ddl-auto: create spring.datasource.driver-class-name: org.h2.Driver server.port: 8384 我的application.yml文件中只有这些
      • 当您尝试访问 URL 但无法打开该页面时,从控制台复制异常。
      猜你喜欢
      • 2016-08-10
      • 1970-01-01
      • 2020-06-07
      • 2011-06-14
      • 2017-02-13
      • 2013-01-08
      • 2016-04-01
      • 2015-01-22
      • 1970-01-01
      相关资源
      最近更新 更多