SlideShare a Scribd company logo
1 of 55
用 Spring boot +
Thymeleaf 建構 EC 平台
江鎧呈 Ken Chiang
Who am I
• 現任職於 Here2shop
• 想要成為超強的 Full stack developer
• 熟悉Java相關開發技術
Spring , Hibernate , Thymeleaf
• 熟悉前端相關開發技術
Webpack , Reactjs , Sass
• 擁有多數電子商務系統和其他平台開發經驗
About Here2shop
• 利用Big Data技術讓商家和消費者更互惠
• 致力達成口碑行銷和顛覆傳統的社交平台
• 你不需要懂電商,因為電商懂你
• 官網 https://www.here2shop.com/
Agenda
• 什麼是 Spring?
• Spring Boot 與以往 Spring 的不同處
• 什麼是 Thymeleaf ?
• Spring Boot 整合 Thymeleaf 應用
• 什麼是 Dandelion ?
• 實際開發經驗分享
• 遇到的開發瓶頸和解決方案
什麼是Spring?
• 有許多 Core Module 整合掉許多程式上開發的困擾
比方說 Spring Web , Spring ORM , Spring AOP 等等
• 透過管理Java Bean來達到Dependency Injection
• 透過IOC(Inversion of Control)達到loose couple,使
得抽換程式更加容易
所以簡單來說Spring就是...
一個盒子裡面裝著許多
Java Bean….
彼此之間透過DI連接著彼此
Controller Layer
Service Layer
DAO Layer & Model Layer
以往每次用Spring開發之前
....
都必須寫那見鬼的ApplicationContext.xml.....
有了 Spring Boot 之後
再也不用寫見鬼的ApplicationContext.Xml檔案
Spring Boot 好處
• 透過AutoConfiguration,就可以達成基本的設定,要額外
Override掉原本設定,只需extends 對應的Adapter即可
• 可以直接利用@Bean annotation,來達成Dependency
Injection直接設定想要使用的Class
• 在不同環境下,可以透過application.property的Override達
到不同的環境設定部署
• 非常適用於部署Micro service,預設Embedded Tomcat能夠
直接用Jar來部署,不需要有Tomcat container
• 透過程式設定一切,不需要再用擾人的xml設定
什麼是 AutoConfiguration
• Spring Boot 會有 default application property,當有使用對應
的Annotation做Enable,就會去使用default的
AutoConfiguration.
• 比如
@EnableTransactionManagement 開啟 Transaction proxy
@EnableRedisHttpSession 開啟 Redis Session Cache
@EnableWebMvcSecurity 開啟 Spring Security
• 如果要Override原本的設定可以透過自己建立Bean或是透過extends
ConfigureAdapter即可
AutoConfiguration 實例
透過 Bean 方式 Initialize Redis Connection & Template
AutoConfiguration 實例
透過 ConfigureAdapter 方式Override WebMvc Config
Application Property
• Spring Boot Default Application Property
Spring boot
Default
Property
application
.properties
application-prod.
properties
override
by
override
by
Deploy to Jar
• Java -jar —spring.profiles.active=h2sdev jar_name.jar
• 指定使用application-h2sdev.property 裡面的設定,override掉
application.property來作部署
什麼是Thymeleaf?
• 是一個 Java Based Template Engine 能夠處理 HTML, XML , javascript , CSS 和
純文字
• 能夠在 Web 和 Non-Web 環境下使用
• 跟 Spring MVC 很好的整合
• 已經把 Standard(JSTL) 和 Spring Standard dialects 整合並且比原本的
JSTL更加強大
• 可以做自己的Tag Library(Dialect)
• 取代 Tiles + JSP
• 可以用作 Html5 email template engine
Spring Boot 整合 Thymeleaf
• 在pom.xml加入下面Dependency
• Default 的 template 路徑位於(可自行Override Bean)
src/main/resources/templates
Thymeleaf Layout
• layout 可以是一個巢狀的結構
• th:include 載入別的fragment到當前tag內
• th:replace 載入別的fragment到替代當前tag
• th:fragment 將當前tag設定成fragment
Thymeleaf Layout cont.
Thymeleaf Layout cont.
layouts/common/base.html
TestController
Standard dialect
Standard dialect cont.
Spring Standard Dialect
• th:action 當有enabled csrf,會在表單後加入csrf token
• field error
• all error
• global error
Spring Standard Dialect
cont.
Spring Validator
Field Error
Sample
什麼是Dandelion?
• 是Bundle static resource的 java framework
• 可以巢狀Bundle
• 根據不同的頁面提供不同的Bundle
達到load必要的css和js file和管理Dependency
• 透過簡單的json定義Bundle
• 擁有debugger可以查看Bundle Dependency graph 和
cache
Bundle Sample
front-base-v1
front-user-v1
dependency
Dandelion Bundle cont.
Dandelion live debugger
實際開發經驗分享
Project Architecture
Front Web
Project
Admin Web
Project
Core Project
Manager
Service
Helper
Model
Utility
Constant
Config
DTO
Library
Controller
Architecture
Controller
(param validation
and
form validation)
Authentication Interceptor
Authorization Interceptor
透過Cookie based Authentication,
每次Request檢查cookie是否合法
透過AOP Annotation
使用在Controller Class或是Method上
,達到檢查不同角色和權限的目的
使用Spring本身的 @Valid 搭配BindingResult
加上自己撰寫的AOP Annotation
檢查Request Param
Authentication Flow
• 透過 Login Page 進行 Login 將 Cookie 寫入
Response
Cookie 產生必須有 Timestamp 和 salt key 用做檢查是否已經過期和增加複雜度
• 設定Interceptor攔截需要Login權限的uri path,
比如所有 /member 底下的路徑
• 當Cookie內的Timestamp過期或是不合法,即
Redirect to Login Page
Authentication Interceptor
Authorization
• 在確認使用者登入後,取得使用者的權限表,透過
AOP Annotation 和 Interceptor 來檢查權限
Controller
個別加程式判斷嗎....
什麼是AOP?
• AOP(Aspect-Oriented Programming),Spring Container
透過 Dynamic Proxy Pattern ( 知道哪些Class要被代理,
但是不知道Runtime要代理誰),在Class呼叫時產生
Proxy.class 去做到代理的動作.
Service 1
Service 2
Service 3
Loggin
g
Authen
tication
Authori
zation
Authorization AOP
RequestParam
Validation
• 因為Spring沒有annotation可以用於檢查
RequestParam,只能透過Bean的@Valid
和 JSR-303 Bean Validation來檢查,所以自己也寫
了一個簡單的Param AOP
如何簡單的做呢....
這邊只做
長度檢查
和Regex檢查
表單被重送了怎
麼辦!
Prevent form duplicate
submission
• 前端在User按下按鈕後,Disabled按鈕
(這只能防止一般使用者操作)
• PRG Pattern (Post/Redirect/Get)
防止使用者按上一頁時,重送表單資料
(這只能防止一般使用者操作)
• 後端擋 —-> 怎麼擋?
Prevent form duplicate
submission cont.
圖片來源 http://terasolunaorg.github.io
CSRF
• 透過Spring Security加上CSRF token 在每次的表單
上,防止被外站引用表單內容重送
圖片來源: https://devnet.kentico.com/articles/protection-against-cross-site-request-forgery-(csrf-xsrf)
Service Architecture
DTO
Redis
Cache
Manager Layer
Service Layer
DAO Layer
Model Layer
Controller Layer
Manager Layer 用途
DTO
Redis
Cache
Manager Layer
Service Layer
DAO Layer
Model Layer
Controller Layer 在不同Service之間有
資料的改變和流程的選
擇時透過都在
Manager Layer
做額外的處理,保持
Service Layer
的單純Business logic
Use DTO Not Model
DTO
Redis
Cache
Manager Layer
Service Layer
DAO Layer
Model Layer
Controller Layer
• 為了用 Json Serializer
進 Redis Cache
• 避免再View Layer還在持續用
hibernate的關聯拉資料,造成
DB session 斷開拉不到資料,且
DB 佔用太長的session
• 易於轉成Json去製作Json API
遇到的開發瓶頸
• 徵才必須一定要是Java Background
• 前後端拆不開...All java
• 每次換Static Resource 都是必須整包Spring Boot打包,換版困難
• javascript 沒有 framework maintain
• Ans : 導入前端開發流程, webpack , reactjs 未來希望把前後端分離,就不侷
限一定要Java Developer
延伸問題:
前台的SEO ?
必須想辦法採用Server Rendering ?
JDK 8 Nashorn Javascript engine ?
遇到的開發瓶頸 cont.
• 每次Core換版,每個WebService都必須換版
• Ans: 使用Tomcat當Web Container,把Core做成Shared
Library
延伸問題:How to deploy to war in different environment config
• 未使用DTO,都使用Model,造成Layer沒有拆分,Redis 也只
能採用 JDKSerializer 資料不可讀,View必須使用
OpenSessionInViewFilter確保Db Session不會斷
• Ans: Refactor….
Thank you for your listening

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

JCCONF2015 - 用 Spring boot + Thymeleaf 建構 EC 平台 by Ken Chiang