本篇文章跟大家介紹一下springboot返回html和jsp的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。
一、返回html
(1)新增maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
(2)thymeleaf模板預設尋找resources下,templates文件夾放html頁面,static資料夾放css及js
(3)引入js,需要使用如下格式
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <script type="text/javascript" th:src="@{/js/jquery/jquery.min.js}"></script> <script type="text/javascript" th:src="@{/js/jquery/jquery.easyui.min.1-7-5.js}"></script> <script type="text/javascript" th:src="@{/js/jquery/easyui-lang-zh_CN.js}"></script> <script type="text/javascript" th:src="@{/js/index.js}"></script> <body> <h2>Hello World!</h2> </body> </html>
(4)controller程式碼如下
package springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HtmlController { @RequestMapping("/show") public String show() { return "aaa"; } }
二、返回jsp
(1)新增jsp的maven依賴
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
註:返回jsp需要把spring-boot-starter-thymeleaf註解掉
(2)在controller裡加入尋找jsp頁面的視圖解析器
@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/"); viewResolver.setSuffix(".jsp"); return viewResolver; }
(3)結構圖如下
(4)controller程式碼如下
package springboot.controller; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Controller public class JspController { @RequestMapping("/test") public String index() { return "home"; } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
註:返回html和jsp時使用@Controller註解
三、載入css 、js檔案
放到static下方
#推薦學習:html影片教學
#以上是springboot如何返回html和jsp的詳細內容。更多資訊請關注PHP中文網其他相關文章!