1. 程式人生 > 其它 >js 匯出 excel window.location.href 沒有生成檔名字的問題

js 匯出 excel window.location.href 沒有生成檔名字的問題

引入

最早開發的時候,展示頁面我們都是使用HTML完成我們的程式碼編寫;但是我們的顯示頁面一定是需要動態變化的,之後就引入了Jsp技術,用來進行資料的顯示及互動,但是Jsp是以war包進行部署,但是之後想用jar包的方式打包,這種方式就會很麻煩,所以就有了模板引擎技術 ,模板引擎有很多,比如jsp,freemarker,thymeleaf等,我們用thymeleaf來舉例

參考地址

官網地址:https://www.thymeleaf.org/

github地址:https://github.com/thymeleaf/thymeleaf

中文網站:https://raledong.gitbooks.io/using-thymeleaf/content/

使用

先引入依賴,我用SpringBoot的starter

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

在springboot中有專門的thymeleaf配置類:ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

   private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

   public static final String DEFAULT_PREFIX = "classpath:/templates/";

   public static final String DEFAULT_SUFFIX = ".html";

   /**
    * Whether to check that the template exists before rendering it.
    */
   private boolean checkTemplate = true;
    
   ..................................
@Controller
public class RequestController {
    @GetMapping("/request")
    public String request(Model model){
        model.addAttribute("msg","name");
        return "show";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${msg}"></h1>
</body>
</html>

標準表示式語法

簡單表示式

表示式名字 語法 用途
變數取值 ${...} 獲取請求域、session域、物件等值
選擇變數 *{...} 獲取上下文物件值
訊息表示式 #{...} 獲取國際化等值
連結URL @{...} 生成連結
片段表示式 ~{...} 同jsp:include 作用,引入公共頁面片段

常量

  • 文字常量 : 'one text', 'Another one!',…
  • 數字常量 : 0, 34, 3.0, 12.3,…
  • 布林常量 : true, false
  • 空常量 : null
  • 常符號 : one, sometext, main,…

文字操作

  • 字串連線 : +
  • 常量替換 : |The name is ${name}|

算數操作

  • 二元運算子:+, -, *, /, %

布林操作

  • 布林操作符 : and, or
  • 布林取反(一元運算子): !, not

比較運算

  • 比較符號: >, <, >=, <= (gt, lt, ge, le)
  • 相等符號: ==, != (eq, ne)

條件操作符

  • If-then: (if) ? (then)
  • If-then-else: (if) ? (then) : (else)
  • 預設值 Default: (value) ?: (defaultvalue)

特殊符號

  • 無操作符 : _

th的常用屬性值

th:text/ th:utext :設定當前元素的文字內容,兩者的區別在於前者不會轉義html標籤,後者會。優先順序不高:order=7

th:value/ th:src/ th:href:設定當前元素的value值,優先順序不高:order=6

th:each:遍歷迴圈元素,和th:text或th:value一起使用。注意該屬性修飾的標籤位置,詳細往後看。優先順序很高:order=2

th:if:條件判斷,類似的還有th:unlessth:switchth:case。優先順序較高:order=3

th:insert/ th:include/ th:replace:程式碼塊引入,三者的區別較大,常用於公共程式碼塊提取的場景。優先順序最高:order=1

th:fragment:定義程式碼塊,方便被th:insert引用。優先順序最低:order=8

th:object:宣告變數,一般和*{}一起配合使用,達到偷懶的效果。優先順序一般:order=4

th:attr/ th:attrappend/ th:attrprepend:修改任意屬性優先順序一般:order=5

所有h5相容的標籤寫法:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

關於屬性的優先順序

thymeleaf內建方法

此外thymeleaf有很多內建方法,與Java的API類似!

  • strings:字串格式化方法,常用的Java方法它都有。比如:equals,length,trim,toUpperCase,indexOf等

  • numbers:數值格式化方法,常用的方法有:formatDecimal等

  • bools:布林方法,常用的方法有:isTrue,isFalse等

  • arrays:陣列方法,常用的方法有:toArray,length,isEmpty,contains,containsAll等

  • lists,sets:集合方法,常用的方法有:toList,size,isEmpty,contains,containsAll,sort等

  • maps:物件方法,常用的方法有:size,isEmpty,containsKey,containsValue等

  • dates:日期方法,常用的方法有:format,year,month,hour,createNow等