Reja: Template engine


dependency > < groupId


Download 2.27 Mb.
Pdf ko'rish
bet2/3
Sana13.11.2023
Hajmi2.27 Mb.
#1771066
1   2   3
Bog'liq
9-Template engine

dependency
>
<
groupId
>org.thymeleafgroupId

<
artifactId
>thymeleafartifactId

<
version
>3.0.11.RELEASEversion
>
dependency

<
dependency

<
groupId
>org.thymeleafgroupId

<
artifactId
>thymeleaf-spring5artifactId

<
version
>3.0.11.RELEASEversion

dependency
>


SpringTemplateEngine beanni talab qiladigan konfiguratsiyani, shuningdek view fayllari joylashuvi va turini 
belgilaydigan TemplateResolver beanni qo`shishimiz kerak. SpringResourceTemplateResolver Springning manbalarni aniqlash 
mexanizmi bilan birlashtirilgan:
Thymeleaf Spring Configuration
@Configuration
@EnableWebMvc
public class
ThymeleafConfiguration

@Bean
public
SpringTemplateEngine 
templateEngine
() { 
SpringTemplateEngine
templateEngine

new
SpringTemplateEngine
(); 
templateEngine.setTemplateResolver(thymeleafTemplateResolver()); 
return
templateEngine; 

@Bean
public
SpringResourceTemplateResolver 
thymeleafTemplateResolver
() { 
SpringResourceTemplateResolver
templateResolver

new
SpringResourceTemplateResolver
();
templateResolver.setPrefix(
"/WEB-INF/views/"
); 
templateResolver.setSuffix(
".html"
); 
templateResolver.setTemplateMode(
"HTML5"
); 
return
templateResolver; 

}


Bundan tashqari, bizga ThymeleafViewResolver turidagi ViewResolver bean kerak :
@Bean
public
ThymeleafViewResolver 
thymeleafViewResolver
() {
ThymeleafViewResolver
viewResolver

new
ThymeleafViewResolver
(); 
viewResolver.setTemplateEngine(templateEngine()); 
return
viewResolver; 
}
Thymeleaf Spring Configuration


Thymeleaf Templates
<
html

<
head

<
meta
charset=
"ISO-8859-1"
/> 
<
title
>User Registrationtitle

head

<
body

<
form
action=
"#"
th:action=
"@{/register}"
th:object=
"${user}"
method=
"post"

Email:<
input
type=
"text"
th:field=
"*{email}"
/> 
Password:<
input
type=
"password"
th:field=
"*{password}"
/> 
<
input
type=
"submit"
value=
"Submit"
/> 
form

body

html
>
Endi biz HTML faylni WEB-INF/views manziliga qo`shishimiz mumkin:


Thymeleaf


Thymeleaf


Thymeleaf shablonlari sintaksis jihatidan HTML shablonlariga juda o`xshash.
Spring ilovasida Thymeleaf dan foydalanishda mavjud bo`lgan ba`zi xususiyatlar :
• shakllarning xatti-harakatini aniqlashni qo`llab-quvvatlash
• ma`lumotlar modellariga shakl kiritishlarini bog`lash
• shakl kiritish uchun tekshirish
• xabar manbalaridan qiymatlarni ko`rsatish
• shablon parchalarini ko`rsatish
Thymeleaf shablonlaridan foydalanish haqida ko`proq o`qishingiz mumkin Thymeleaf
in Spring MVC .
Thymeleaf Templates


Thymeleaf
Spring boot Dependencies
Spring Boot spring-boot-starter-thymeleaf dependency qo`shish orqali Thymeleaf uchun 
avtomatik konfiguratsiyani ta`minlaydi. Hech qanday aniq konfiguratsiya shart 
emas. Odatda, HTML fayllari resources/templates joylashgan joyga joylashtirilishi kerak.
<
dependency

<
groupId
>org.springframework.bootgroupId
>
<
artifactId
>spring-boot-starter-thymeleafartifactId
>
<
version
>2.5.6version
>
dependency
>


Freemaker
FreeMarker - bu Apache Software Foundation tomonidan yaratilgan Javaga asoslangan shablon 
mexanizmi. U web-sahifalarni yaratish uchun ishlatilishi mumkin, shuningdek, manba kodi, XML 
fayllari, konfiguratsiya fayllari, elektron pochta xabarlari va boshqa matnga asoslangan 
formatlardan tashkil topishi mumkin.


Freemaker Maven Dependencies
Loyihamizda shablonlardan foydalanishni boshlash uchun bizga freemarker dependency
va Spring integratsiyasi uchun bizga Spring kontekstiga bog`liqlik kerak:
<
dependency
>
<
groupId
>org.freemarkergroupId

<
artifactId
>freemarkerartifactId

<
version
>2.3.23version
>
dependency
>
<
dependency

<
groupId
>org.springframeworkgroupId

<
artifactId
>spring-context-supportartifactId

<
version
>5.2.8.RELEASEversion

dependency
>


Freemaker Spring Configuration
FreeMarkerni Spring MVC bilan integratsiya qilish template fayllari joylashuvini
belgilaydigan FreemarkerConfigurer beanni aniqlashni talab qiladi:
@Configuration
@EnableWebMvc
public class
FreemarkerConfiguration

@Bean
public
FreeMarkerConfigurer 
freemarkerConfig
() { 
FreeMarkerConfigurer
freeMarkerConfigurer

new
FreeMarkerConfigurer
(); 
freeMarkerConfigurer.setTemplateLoaderPath(
"/WEB-INF/views/"
); 
return
freeMarkerConfigurer; 

}


Freemaker Spring Configuration
Keyin, FreeMarkerViewResolver turidagi tegishli ViewResolver beanni aniqlashimiz kerak :
@Bean
public
FreeMarkerViewResolver 
freemarkerViewResolver
() { 
FreeMarkerViewResolver
resolver

new
FreeMarkerViewResolver
(); 
resolver.setCache(
true
); 
resolver.setPrefix(
""
); 
resolver.setSuffix(
".ftl"
); 
return
resolver;
}


FreeMarker Templates
WEB - INF/views manzilida FreeMarker yordamida HTML shablonlarini yaratishimiz mumkin :
<#import "/spring.ftl" as spring/> 
<
html

<
head
>
<
meta
charset=
"ISO-8859-1"
/> 
<
title
>User Registrationtitle

head

<
body

<
form
action=
"register"
method=
"post"

<@spring.bind path="user" /> 
Email: <@spring.formInput "user.email"/> 
Password: <@spring.formPasswordInput "user.password"/> 
<
input
type=
"submit"
value=
"Submit"
/> 
form

body

html
>


Yuqoridagi misolda biz FreeMarker da shakllar bilan ishlash uchun Spring
tomonidan belgilangan makrolar to`plamini import qildik , shu jumladan
ma`lumotlar modellari uchun shakl kiritishlarini bog`lash.
Bundan tashqari, FreeMarker shablon tili to`plamlar, oqimlarni boshqarish
tuzilmalari, mantiqiy operatorlar, formatlash va tahlil qilish satrlari, raqamlar va
boshqa ko`plab funksiyalar bilan ishlash uchun juda ko`p teglar, direktivalar va
ifodalarni o`z ichiga oladi.
FreeMarker


Spring Boot ilovasida biz
spring-boot-starter-freemarker
dependencydan foydalanib,
kerakli konfiguratsiyani soddalashtirishimiz mumkin. Ushbu dependency kerakli avtomatik
konfiguratsiyani
qo`shadi.
Biz
qilishimiz
kerak
bo`lgan
narsa
template
fayllarimizni resources/templates jildiga joylashtirishdan boshlashdir.
<
dependency

<
groupId
>org.springframework.bootgroupId

<
artifactId
>spring-boot-starter-freemarkerartifactId

<
version
>2.5.6version

dependency
>
FreeMarker in Spring Boot


Velocity
Velocity - bu Javaga asoslangan template engine hisoblanadi. Bu MVC arxitekturasida view
komponenti sifatida foydalanish uchun mo`ljallangan ochiq manbali web-framework bo`lib, u 
JSP kabi ba`zi mavjud texnologiyalarga muqobil hisoblanadi.
Velocity XML fayllari, SQL, PostScript va boshqa matnga asoslangan formatlarni yaratish 
uchun ishlatilishi mumkin.
Velocity qanday ishlaydi?
Velocity ning asosiy klassi VelocityEngine hisoblanadi . U ma`lumotlar modeli va tezlik 
shablonidan foydalangan holda o`qish, tahlil qilish va tarkibni yaratishning butun jarayonini 
tartibga soladi. Oddiy qilib aytganda, har qanday odatiy tezlik ilovasi uchun amal qilishimiz 
kerak bo`lgan qadamlar quyidagilar:
• Velocity Engineni ishga tushirish
• Templateni o`qish
• Ma`lumotlar modelini konteks ob`ektiga qo`yish
• Templateni konteks ma`lumotlari bilan birlashtirish va ko`rinishni yaratish


Velocity
Apache Velocity - birinchi marta 2001-yil aprel oyida chiqarilgan, Javaga
asoslangan template engine bo`lib , Java kodida belgilangan ob`ektlarga havola
qilish uchun template tilini taqdim etadi. Bu web-ilovada taqdimot darajasi va
biznes darajalari o`rtasida aniq ajratishni ta`minlashga qaratilgan ( MVC dizayn
namunasi).
Velocity ochiq kodli dasturiy ta`minot loyihasi bo`lib, Apache Software
Foundation tomonidan joylashtirilgan . U Apache litsenziyasi ostida chiqariladi.


Velocity



Download 2.27 Mb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling