解決SpringBoot項目通過Controller的return沒法跳轉頁面的問題

Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers

這個是我的報錯。

我在SpringBoot項目中的Controller中通過return跳轉頁面的時候無法跳轉,項目報上面的異常。首先我把Controller類中的註解@RestController修改爲了@Contrller,但是沒有什麼作用,接着我在Controller類中的方法上面添加了註解@Responsebody,這個時候發現頁面還是沒有跳轉成功,但是頁面直接打印出來我返回的內容。隨後我檢查了自己的項目目錄,關鍵是templates的位置,對比了我的配置文件application.yml中關於thymeleaf的配置。

仔細對照,並沒有什麼不對的地方。然後我查看了添加的依賴,我的依賴是

<dependency>       

    <groupId>org.springframework.boot</groupId>      

    <artifactId>spring-boot-starter-thymeleaf</artifactId>    

</dependency>

依賴也沒有問題。最後我注意到我的pom.xml中的build中有一段配置

這段配置是爲了項目能夠掃描到xml或者priperties而配置的,其實並沒有什麼額外的作用,但是也就是這段配置導致找不到我的html頁面,將他們註釋掉就好了,或者在裏面添加上html文件類型就好,通過Controller中的return就可以正常跳轉頁面了。

Controller類中的跳轉方法:

@RequestMapping(value = "/index")
@ResponseBody
public ModelAndView index(Model model){
    model.addAttribute("boot_name","jack");
    model.addAttribute("boot_age",20);
    model.addAttribute("boot_info","jack,success");
    return new ModelAndView("index");
}

添加html類型如下:

<!--保證配置文件能被掃描到-->
<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.yml</include>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <includes>
      <include>**/*.yml</include>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
      <include>**/*.html</include>
    </includes>
  </resource>
</resources>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章