Spring boot 訪問靜態頁面!

SpringBoot訪問靜態頁面
 一、靜態頁面放在templates下面(多個頁面可以添加相應的文件夾,方便管理):






二、配置Controller

這裏有兩點值得注意:

    其一:類註解不能用RestController,只能用Controller

        restController和controller的區別:

@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
意思是:
@RestController註解相當於@ResponseBody(方法) + @Controller(類)合在一起的作用。

1)使用@RestController註解,方法無法返回頁面,配置的視圖解析器InternalResourceViewResolver不起作用,返回的內容就是Return 裏的內容。如果需要返回json或者xml或者自定義mediaType內容到頁面,在對應的方法上加上@ResponseBody註解即可(或者直接在類上添加@RestController)。

例如:本來應該到success.html頁面的,則其顯示/success.

2)如果需要返回到指定頁面,則僅僅需要用 @Controller配合視圖解析器InternalResourceViewResolver才行。

    其二、如果有多個頁面,則需要添加上路徑。

    返回templates下test的index.html:return “/test/index;

三、html中的配置一定要正確


遇到如下問題There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/users/list.html]")

本次出現這個的原因在於thymeleaf模板佈局代碼出現了問題,相關變量的值配置不正確。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章