Java爲什麼不返回jsp頁面或無法訪問jsp頁面,只返回了頁面的名字

你的controller類上使用了@RestController

改成@Controller

@Controller:標識一個Spring類是Spring MVC controller處理器

@RestController: @RestController是@Controller和@ResponseBody的結合體,兩個標註合併起來的作用。

如果只是使用@RestController註解Controller,則Controller中的方法無法返回jsp頁面,配置的視圖解析器InternalResourceViewResolver不起作用,返回的內容就是Return 裏的內容。

例如:本來應該到products.jsp頁面的,則其顯示products.

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

5、如果需要返回JSON,XML或自定義mediaType內容到頁面,則需要在對應的方法上加上@ResponseBody註解。

 

發佈了21 篇原創文章 · 獲贊 22 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章