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

