1. 程式人生 > >SpringBoot跳轉頁面詳解+thymeleaf

SpringBoot跳轉頁面詳解+thymeleaf

        初次做SpringBoot,要解決頁面跳轉的問題,這個問題我弄了大半天,弄好後,其實也不算個事,寫出來給大家提個醒!其實不要使用spring boot的@RestController註解,直接使用spring原來的註解@Controller就可以了。示例如下:

@Controller

public class ActionController {

    @RequestMapping(value = "/action",method = RequestMethod.GET)

    public String index(){

        return "login";

    }

}

如果你的專案如什麼都沒有配,那麼你想跳到login.html時,語句必須是  return "login.html"  ,否則它會報錯 。因為找不到login檔案。

        為什麼呢?因為@RestController註解,相當於@[email protected]兩個註解的結合, @Responsebody後,返回結果直接寫入HTTP response body中,不會被解析為跳轉路徑,所以你總是看到是列印字串的效果,不是跳轉效果。

=========================================================

        請看更詳細的解答:SpringBoot裡面沒有我們之前常規web開發的WebContent(WebApp),它只有src目錄,在src/main/resources下面有兩個資料夾,[static]和[templates],springboot預設static中放靜態頁面,而templates中放動態頁面,見下圖:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406104541721-1302659819.png

靜態頁面:

        靜態頁面可以直接訪問。這裡我們直接在static放一個hello.html,然後直接輸入http://localhost:8080/hello.html便能成功訪問(好像可以新建一個public資料夾,也可以放靜態檔案)。

        也可以通過controller跳轉:

@Controller

public class HelloController {

    @RequestMapping("/Hi")

    public String sayHello() {

        return "hello.html";

    }

}

        然後輸入http://localhost:8080/Hi就可以成功訪問 

動態頁面:

        動態頁面需要先請求伺服器,訪問後臺應用程式,然後再轉向到頁面,比如訪問JSP。spring boot建議不要使用JSP,預設使用Thymeleaf來做動態頁面。

要在pom中要新增Thymeleaf元件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

但你加上了上面的thymeleaf後,你必須重啟工程,即使是你配置了熱啟動後,也要重啟工程,才可以看到效果 。

    我們先在tempates資料夾中也新建一個hello.html但內容不同,然後先試一下直接訪問該頁面。輸入http://localhost:8080/hello.html:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406105509468-1935985534.png

結果顯然訪問的是靜態問價夾裡面的那個hello.html

然後我們現在再試一下用controller:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406105655969-540195122.png

似乎無法訪問到hello.html了。。。這是因為:靜態頁面的return預設是跳轉到/static/index.html,當在pom.xml中引入了thymeleaf元件,動態跳轉會覆蓋預設的靜態跳轉,預設就會跳轉到/templates/index.html,注意看兩者return程式碼也有區別,動態有或沒有html字尾都可以。

也就是我們要這樣改controller:

@Controller

public class HelloController {

@RequestMapping("/Hi")

    public String sayHello() {

        return "hello";//在沒有配置的情況下,return "hello”; 或者return "hello"都可以,它們都會到templates/index.html去。

    }  

}

然後就可以成功跳轉了

然後我們看看返回一點資料在前端利用Thyemleaf來拿:

@Controller

public class HelloController {

    @RequestMapping("/Hi")

    public ModelAndView sayHello() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("hello");

        modelAndView.addObject("key", 12345);

        //System.out.println("test");

        return modelAndView;

    }

}

---------------------

Templates/hello.html

---------------------

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<title>Insert title here</title>

</head>

<body>

<h1>this is the hello.html in templates</h1>

<span th:text="${key}"></span> 

</body>

</html>

效果:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406115915727-969075466.png

如果不想返回檢視,則用@RestController

如果用了靜態模板你還想返回static中的頁面,那麼就要用重定向:

如果在使用動態頁面時還想跳轉到/static/index.html,可以使用重定向return "redirect:/index.html"。 

幾點tips:

1.攔截的url最後不要跟檢視重合,否則會丟擲Circular view path異常,我之前就是

@Controller

public class HelloController {

    @RequestMapping("/hello")

    public String sayHello() {

        return "hello.html"; 

    }

}

然後就報錯說會有個迴圈檢視的錯誤,反正以後注意就是。

2.每次改完都要重新停止應用,再重新啟動很煩~但springboot有個叫熱部署的東西,就是說在專案中修改程式碼可以不用重新停止應用再重新啟動,可以自動重啟,這裡我們用的是devtools:

sts熱部署,即是在專案中修改程式碼不用重新啟動服務,提高效率。

方法如下:

1.pom檔案中引入  devtools  依賴:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <!-- optional=true, 依賴不會傳遞, 該專案依賴devtools; 之後依賴boot專案的專案如果想要使用devtools, 需要重新引入 -->
  <optional>true</optional>
</dependency>

2.application.properties  檔案中碼上以下內容:

spring.thymeleaf.cache=true        //快取
spring.devtools.restart.enabled=true   //開啟
spring.devtools.restart.additional-paths=src/main/java  //監聽目錄

3.原理

<!-- devtools可以實現頁面熱部署(即頁面修改後會立即生效,
這個可以直接在application.properties檔案中配置spring.thymeleaf.cache=false來實現) -->
<!--
實現類檔案熱部署(類檔案修改後不會立即生效),實現對屬性檔案的熱部署。 -->
<!--
devtools會監聽classpath下的檔案變動,並且會立即重啟應用(發生在儲存時機),
注意:因為其採用的虛擬機器機制,該項重啟是很快的 -->
<!--
1base classloader Base類載入器):載入不改變的Class,例如:第三方提供的jar包。 -->
<!--
2restart classloaderRestart類載入器):載入正在開發的Class -->
<!--
為什麼重啟很快,因為重啟的時候只是載入了在開發的Class,沒有重新載入第三方的jar包。 -->

-----------------------------------

如何使用thymeleaf [taim li:f] 來達到以前jsp獲取Model值這樣的效果(thymeleaf是什麼?你自行百度),那麼,你得在maven中加依賴:

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

然後,在配置檔案:application.yml或application.property中加配置:

spring.thymeleaf.prefix:classpath: /templates/

spring.thymeleaf.suffix: .html

spring.thymeleaf.mode: HTML

spring.thymeleaf.encoding: utf-8

spring.thymeleaf.cache: false

再寫控制類時把Model加在引數中:

@Controller
public class ActionController {

    @RequestMapping(value = "/action",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("aaa","我是一個兵");
        model.addAttribute("bbb","來自老百姓!");
        return "index";
    }
}

它就會跳轉到/templates/index.html這個網頁上去了。

你的index.html如果想顯示"我是一個兵","來自老百姓"的值的話,網頁應該這樣寫:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">    <!-- 聽說這一句要,但我沒有要這個標籤,也能用  -->
<head>
<body>
<h1>AAAAindex.jspsdfasd</h1>
<p>Today is: <span th:text="${aaa}">13 february 2011</span>.</p>   <!-- 不能直接${aaa},而應該寫在一個標籤中 -->
@{${aaa}}<br> <!--這裡是取不到值的 -->
${aaa}<br>    <!--這裡是取不到值的 -->
${bbb}        <!--這裡是取不到值的 -->

</body>
</html>

-------------------------------------------

相關推薦

SpringBoot頁面+thymeleaf

        初次做SpringBoot,要解決頁面跳轉的問題,這個問題我弄了大半天,弄好後,其實也不算個事,寫出來給大家提個醒!其實不要使用spring boot的@RestController註解,直接使用spring原來的註解@Controller就可以了。示例如下:

ionic3頁面步驟

前言:   最近接手了一個公益A手機PP,由於第一次使用ionic開發前端,所以對好多功能不太熟悉,今天就解決了一個關於頁面跳轉的問題。一、建立頁面:    $Ionic g page new     

五種JSP頁面方法

  1. RequestDispatcher.forward() 是在伺服器端起作用,當使用forward()時,Servlet engine傳遞HTTP請求從當前的Servlet or JSP到另外一個Servlet,JSP 或普通HTML檔案,也即你的form提交至a.

springboot頁面報錯

his application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Nov 15 16:22:13 CST 2017 There was an unexp

springboot頁面

在springboot controller跳轉時請注意 分以下三個步驟 步驟一 在pom.xml新增相關依賴 <!-- 引入了Spring Boot預設的HTTP引擎Tomcat。 --> <dependency> <groupId&g

SpringBoot渲染HTML頁面

本文的例項程式碼也會放在文末分享給大家的。正文開始: 目錄 1、 規則 2、 示例 (1) 原理 一、直接訪問 直接訪問說白了就是訪問html頁面的地址前面是沒有資料夾的。如下圖 1、新建一個Spr

4 如何配置springboothtml頁面(thymeleaf)

  注意: jss .css預設去static檔案中取找             html預設去templates中取去找    1、首先在pom.xml新增對HTML的相關依賴 /*

springboot簡單專案搭建遇到的錯誤(繼承thymeleaf)--頁面失敗(不應用ModelAndView物件,採取配置)

我這個一直就沒跳轉到welcome的那個靜態頁面,只是返回了個welcome字串,見笑見笑,後來發現是Controller註解用錯了我原來用的是@RestController,圖中是改完好使的@Controller。@RestController是@Controller和@R

springboot編寫一個頁面

pom.xml檔案 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLS

springboot至html頁面配置

1,首先在pom檔案中引入模板引擎jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-

SpringBoot之定時任務

pac multi mpi 例如 mark tor tro size har 序言 使用SpringBoot創建定時任務非常簡單,目前主要有以下三種創建方式: 一、基於註解(@Scheduled) 二、基於接口(SchedulingConfigurer) 前者相信

SpringBoot入門 - 使用Freemaker模板頁面

1.專案匯入FreeeMarker模板引擎所需依賴  (springboot所需依賴以及其他的web支援等依賴就根據自己的專案來匯入即可) <!-- FreeeMarker模板引擎所需依賴 --> <dependency> <groupId&g

【2017-05-21】WebForm跨頁面傳值取值、C#服務端頁面、 Button的OnClientClick屬性、Js中getAttribute和超鏈接點擊彈出警示框。

代碼 height delet update size 內存 客戶 bar win 一、跨頁面傳值和取值: 1、QueryString - url傳值,地址傳值 優缺點:不占用服務器內存;保密性差,傳遞長度有限。 通過跳轉頁面路徑進行傳值,方式: href="地址?ke

apicloud 點擊頁面

png nbsp function script click type img 聯系 owin <div class="lianxi"> <button onclick="opnew()">聯系商家</button> <

php curl 請求302頁面

turn follow 自動跳轉 接口 exe col dump details useragent 今天對接支付接口,需要獲取支付頁面,發現支付商那邊給的鏈接會發送302 跳轉,最後發現該方法,絕對給力: <?php $url = ‘http://auto.jrj

JS的window命令頁面

src com images 跳轉頁面 cnblogs ges 技術分享 alt 命令 JS的window命令跳轉頁面

微信瀏覽器頁面後再返回,如何恢復到前的位置的問題。

客戶端 his ont 恢復 func 詳情 size light list 以商品列表頁打比方, 眾所周知,點擊商品進入詳情頁要保證不損壞當前列表頁狀態的做法通常是在a標簽上加上target=_blank進行新開一個窗口打開詳情頁 這個做法是非常普遍的,包括很多

()Java JVM 工作原理和流程

移植 獲得 代碼 適配 調用 tac 階段 main方法 等待 作為一名Java使用者,掌握JVM的體系結構也是必須的。說起Java,人們首先想到的是Java編程語言,然而事實上,Java是一種技術,它由四方面組成:Java編程語言、Java類文件格式、Java虛擬機和Ja

jq 鼠標點擊頁面後 改變點擊菜單的樣式代碼

pre src cat ges 沒有 blog 用戶 添加 ive 點擊菜單跳轉頁面,然而跳轉後的頁面字體並沒有加粗用如下代碼 <div class="bg01 menu"> <img class="img01"

JS頁面常用的幾種方法

class define bst top color defined rip dex text 第一種:(常用) <script language="javascript" type="text/javascript"> window.location.