springboot返回html和jsp的方法示例
一、返回html
(1)添加maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
(2)thymeleaf模板默認(rèn)尋找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)結(jié)構(gòu)圖如下
(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時(shí)使用@Controller注解
到此這篇關(guān)于springboot返回html和jsp的方法示例的文章就介紹到這了,更多相關(guān)springboot返回html和jsp內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決spring-boot2.0.6中webflux無法獲得請求IP的問題
這幾天在用 spring-boot 2 的 webflux 重構(gòu)一個(gè)工程,寫到了一個(gè)需要獲得客戶端請求 IP 的地方,在寫的過程中遇到很多問題,下面小編通過一段代碼給大家介紹解決spring-boot2.0.6中webflux無法獲得請求IP的問題,感興趣的朋友跟隨小編一起看看吧2018-10-10springboot使用war包部署到外部tomcat過程解析
這篇文章主要介紹了springboot使用war包部署到外部tomcat過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
這篇文章主要介紹了SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法,Spring對數(shù)據(jù)庫的操作在jdbc上s面做了深層次的封裝,使用spring的注入功能,可以把DataSource注冊到JdbcTemplate之中。下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-02-02深入淺析springsecurity入門登錄授權(quán)
SpringSecurity為我們提供了基于注解的權(quán)限控制方案,這也是我們項(xiàng)目中主要采用的方式,我們可以使用注解去指定訪問對應(yīng)的資源所需的權(quán)限,這篇文章主要介紹了springsecurity入門登錄授權(quán),需要的朋友可以參考下2024-05-05如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境)
spark程序可以通過pom.xml的文件配置,添加spark-core依賴,可以直接在IDEA中編寫spark程序并運(yùn)行結(jié)果,這篇文章主要介紹了如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境),需要的朋友可以參考下2024-02-02