Spring Boot 整合視圖層技術,application全局配置文件

来源:https://www.cnblogs.com/joker-dj/archive/2020/04/08/12657530.html
-Advertisement-
Play Games

"TOC" Spring Boot 整合視圖層技術 Spring Boot 整合jsp Spring Boot 整合Freemarker Spring Boot 整合 Thymeleaf (重點講解,官方推薦) Spring Boot 整合jsp 步驟: 1. 新建maven project的Spr ...


目錄

Spring Boot 整合視圖層技術

Spring Boot 整合jsp
Spring Boot 整合Freemarker
Spring Boot 整合 Thymeleaf  (重點講解,官方推薦)

Spring Boot 整合jsp

步驟:

  1. 新建maven project的Spring Boot 的jar項目
    在這裡插入圖片描述
    在這裡插入圖片描述
  2. 打開pom.xml文件 加入jsp依賴
    在這裡插入圖片描述
    代碼如下:
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <dependencies>
        <!-- spring boot web啟動器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- jasper:jsp引擎 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>

  1. 編寫控制器Controller(不訪問資料庫)
    在這裡插入圖片描述
    代碼如下:
@Controller
public class UserController {
	/**
	 * 獲取用戶信息,到jsp頁面進行展示
	*/
	@RequestMapping("/userList")
	public String getUsersAll(Model model) {
		//訪問業務層-->數據訪問層mapper-->mybatis資料庫獲取所有用戶信息
		//模擬,定義固定的用戶信息
		List<User> list=new ArrayList<User>();
		list.add(new User("007", "小張", 22));
		list.add(new User("009","小康",32));
		list.add(new User("012","小健",18));
		model.addAttribute("list", list);
		//配置springmvc的視圖解析器,首碼:/WEB-INF/   尾碼: .jsp
		return "index";
	}
}

  1. 創建Spring Boot的全局配置文件 application.properties
    src/main/resources-->創建-->application.properties
    Spring boot預設識別兩個全局配置文件:application.properties和application.yml
    在這裡插入圖片描述
    代碼:
#配置jsp的訪問的首碼和尾碼 (視圖解析器)
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

  1. 視圖層 jsp
    src/main-->webapp-->WEB-INF-->index.jsp
    在這裡插入圖片描述
    代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用戶顯示頁面</title>
</head>
<body>
<table border="1" width="60%" align="center">
    <tr>
        <td>用戶編號</td>
        <td>用戶名稱</td>
        <td>年齡</td>
    </tr>
    <c:forEach items="${list}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.age}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

  1. 啟動類
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
  1. 運行 瀏覽器輸入 localhost:8080/userList
    在這裡插入圖片描述

Spring Boot 整合freemarker

  1. 創建maven project 的jar 的spring boot 項目 (步驟一樣省略)
  2. 打開pom.xml,加入freemarker相關依賴
    在這裡插入圖片描述
    代碼:
<dependencies>
     <!--spring boot web 啟動器坐標 -->
     <dependency>
     	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     
     <!-- freemarker 啟動器坐標 -->
     <dependency>
     	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-freemarker</artifactId>
     </dependency>
  </dependencies>

  1. 編寫控制器Controller
    在這裡插入圖片描述
  2. 視圖層 freemarker
    freemarker 頁面必須放入src/main/resources下的templates目錄下,並且頁面的擴展名為:ftl
    在這裡插入圖片描述
    代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用戶顯示頁面</title>
</head>
<body>
   <table border="1" width="60%" align="center">
       <tr>
       	  <td>用戶編號</td>
       	  <td>用戶名稱</td>
       	  <td>年齡</td>
       </tr>
	   <!--freemarker獲取request傳過來的數據  <#數據類型  key類型 as 遍歷元素名稱>-->
	   <#list list as user>
	       <tr>
	       	  <td>${user.id}</td>
	       	  <td>${user.username}</td>
	       	  <td>${user.age}</td>
	       </tr>
	   </#list>
   </table>
</body>
<html>

  1. 創建Spring Boot的全局配置文件 application.properties
    在這裡插入圖片描述
    代碼:
# 模板編碼。
spring.freemarker.charset= UTF-8
# 尾碼,在構建URL時附加到查看名稱。
spring.freemarker.suffix=.ftl
# 逗號分隔的模板路徑列表。src/main/resources==classpath
spring.freemarker.template-loader-path=classpath:/templates/
server.port=8081
  1. 啟動類
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

運行
在這裡插入圖片描述


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1. 圖片 <img src='圖片的地址' />:最後的 / 可以不加,HTML5不加 2. 音頻 <audio 屬性=" "> <source src=" 音頻的地址" type="類型" > </audio> 各個瀏覽器所支持音頻的格式: 示例: 3. 視頻 <video width = ' ...
  • 1. head 標記 <title> 標題 </title>:當前網頁標題 <style> CSS代碼 </style>:寫CSS用的 <link href = " css 文件的地址" rel = " stylesheet " />:引入css文件 <script> javascript 代碼 < ...
  • 1. 表單標記 表單作用:為了收集用戶信息的作用,如:登錄、註冊、搜索 文本框 <input type = " text " name = " 名稱 " /> 密碼框 <input type = " password " name = " 名稱 " /> 單選框 <input type = " ra ...
  • 1. 你們系統里為什麼要使用消息隊列? 2. 既然使用了消息隊列,說說他還有什麼使用場景? 3. 消息隊列的優缺點是什麼? ...
  • 所有代碼和筆記均可在 "我的GitHub" 中獲取,不嫌麻煩可以點個 star 支持一下
  • 仿射期限結構模型:理論與實現——實現部分 [toc] 本文介紹如何以面向對象的方式實現 " Affine Term Structure Models: Theory and Implementation " 中的演算法,並適當的使用設計模式使代碼儘可能的優雅。 引言 金融工程領域的模型和方法之間既有強 ...
  • "TOC" 系統架構 概述 隨著互聯網的發展,網站應用的規模不斷擴大。需求的激增,帶來的是技術上的壓力。 系統架構也因此也不斷的演進、升級、迭代。 從單一應用,到垂直拆分,到分散式服務,到SOA,以及現在火熱的微服務架構 集中式架構 概述 當網站流量很小時,只需一個應用 將所有功能都部署在一起,以減 ...
  • "TOC" Spring Boot整合Thymeleaf Spring Boot整合Thymeleaf(Spring Boot官方推薦的視圖層技術) Thymeleaf特點:thymeleaf通過特定的語法對html的標記進行渲染。 Spring Boot整合Thymeleaf 的項目步驟 1. 創 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...