±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚ΢ÐÅ£¬±¾ÎÄÖ÷Òª½éÉÜÁËthymeleaf¾¿¾¹ÊÇʲô,springÎªÊ²Ã´ÍÆ¼öÓÃËü,Ï£Íû¶ÔÄúÄÜÓÐËù°ïÖú¡£
|
|
Ò»¡¢thymeleaf¼ò½é£º
thymeleafÊÇÒ»ÖÖJavaÄ£°åÒýÇæ£¬ÄǺÎΪģ°åÒýÇæÄØ£¿Ä£°åÒýÇæ¾ÍÊÇΪÁËʹÓû§Ò³ÃæºÍÒµÎñÊý¾ÝÏ໥·ÖÀë¶ø³öÏֵģ¬ ½«´Óºǫ́·µ»ØµÄÊý¾ÝÉú³ÉÌØ¶¨µÄ¸ñʽµÄÎĵµ£¬ÕâÀï˵µÄÌØ¶¨¸ñʽһ°ã¶¼Ö¸HTMLÎĵµ¡£ËüÄܹ»´¦Àíhtml¡¢xml¡¢js¡¢cssÉõÖÁ´¿Îı¾£¬ÀàËÆÓÚfreemarker¡£ËüµÄÓŵãÊÇÓï·¨ÓÅÑÅÒ×¶®¡¢ÔÐͼ´Ò³Ãæ¡¢×ñ´Óweb±ê×¼¡£ÔÐͼ´Ò³ÃæÊÇËüµÄÌØÉ«£¬ËùνÔÐͼ´Ò³Ã棬¾ÍÊÇÄãдµÄhtml£¬¾²Ì¬µÄÈ¥·ÃÎÊÊÇʲôÑù£¬¶¯Ì¬µÄÈ¥·ÃÎÊ»¹ÊÇÕâÑù£¬Ö»²»¹ý¶¯Ì¬µÄʱºò»á°ÑÊý¾ÝÌî³ä½øÈ¥¡£
¶þ¡¢thymeleaf±ê×¼·½ÑÔ£º
1¡¢±äÁ¿±í´ïʽ£º${...}ÀýÈçǰ¶Ë½ÓÊÕÒ»¸öuser,ÏëÈ¡³öuserµÄnameÊôÐÔ£¬¾Í¿ÉÒÔÓñäÁ¿±í´ïʽ£º
<span th:text="${user.name}"> |
2¡¢ÏûÏ¢±í´ïʽ£º#{...}Ò²³ÆÎªÎı¾Íⲿ»¯¡¢¹ú¼Ê»¯»òi18n.
<p th:text="
# {header.address.city}">...</p> |
3¡¢Ñ¡Ôñ±í´ïʽ£º*{...}Óë±äÁ¿±í´ïʽµÄÇø±ð£ºÑ¡Ôñ±í´ïʽÊÇÔÚµ±Ç°Ñ¡ÔñµÄ¶ÔÏóÉÏÖ´Ðжø²»ÊÇÕû¸öÉÏÏÂÎÄ¡£
<form
action
=
"/users"
th:action
=
"@{/users}"
method
=
"POST"
th:object
=
"${userModel.user}"
>
<input
type
=
"hidden"
name
=
"id"
th:value
=
"*{id}"
>
</form> |
ÕâÀïid¾ÍÓÃÁËÑ¡Ôñ±í´ïʽ£¬ÔÚ´Ë´¦ *{id}Óë ${userModel.user.id}Ч¹ûÒ»Ñù¡£
4¡¢Á´½Ó±í´ïʽ£º@{...}url¿ÉÒÔÊÇÏà¶ÔµÄ£¬Ò²¿ÉÒÔÊǾø¶ÔµÄ¡£
<a th:href="@{.../users/list}">...</a>
<a th:href="@{http://www.baidu.com}">...</a> |
5¡¢·Ö¶Î±í´ïʽ£ºth:insert ¡¢th:replace ¡¢th:include¾ÍÏ൱²åÈë¡£ÕâÈý¸öµÄÇø±ð£º
ÏÖÓÐÒ»¸öƬ¶ÎÈçÏ£º
<footer th:fragment="copy">
<h1>Hello Thymeleaf </h1>
</footer> |
#ºÅ·Ö±ð´ú±íinsert¡¢replace¡¢include½øÐвÙ×÷£º
<div th:#
="footer :: copy"></div> |
th:insert µÄ½á¹û£º
<div>
<footer th:fragment="copy">
<h1>Hello Thymeleaf </h1>
</footer>
</div> |
°Ñfooter±êÇ©²åÈëµ½ÁËdiv±êÇ©ÖС£
th:replaceµÄ½á¹û£º
<footer th:fragment="copy">
<h1>Hello Thymeleaf </h1>
</footer> |
°Ñdiv±êÇ©»»³ÉÁËfooter±êÇ©¡£
th:includeµÄ½á¹û£º
<div>
<h1>Hello Thymeleaf </h1>
<div> |
°Ñdiv±êÇ©ÀïÃæµÄÄÚÈÝ»»³ÉÁËfooter±êÇ©ÀïÃæµÄÄÚÈÝ¡£3.X°æ±¾ºó²»ÔÙÍÆ¼öʹÓá£
6¡¢×ÖÃæÁ¿£º×ÖÃæÁ¿¿ÉÒÔÊÇÎı¾¡¢Êý×Ö¡¢²¼¶ûºÍnullµÈÀàÐÍ¡£
7¡¢ËãÊõ²Ù×÷£º+¡¢-¡¢*¡¢/¡¢%ÀýÈ磺
<div th:with=
"isEven=(${user.age} % 2 == 0)"> |
8¡¢ÆäËûÔËËã·û£º±È½Ï£º >¡¢>¡¢\¡¢>¡¢=¡¢\<= (gt¡¢lt¡¢ge¡¢le)µÈ¼Û£º ==¡¢!= (eq¡¢ne)ÈýÄ¿ÔËËã·û£º
<tr
th:class
=
"${row.even} ? 'even' : 'odd' "
></tr> |
9¡¢µü´úÆ÷£ºth:eachÏ൱ÓÚJavaµÄforeach.
<tr th:each="user
: ${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.email}"></td>
</tr> |
ÕâÑù¾ÍÊDZéÀúuserList¼¯ºÏ¡£
µü´úÆ÷µÄ״̬±äÁ¿ÓУºindex¡¢count¡¢size¡¢current¡¢even/odd¡¢first¡¢last
10¡¢Ìõ¼þÓï¾ä£ºth:if¡¢th:unless¡¢switch
<div th:switch="${user.role}">
<p th:case=" 'admin' ">User
is admin</p>
<p th:case=" 'guest' ">User
is guest</p>
</div> |
11¡¢Ä£°å²¼¾Ö£ºth:fragment±ÈÈ綨ÒåÒ»¸ö¹«ÓõÄҳͷ£º
<div th:fragment="header">
<h1>Thymeleaf in action</h1>
<a href="/users">Ê×Ò³</a>
</div> |
ÔÚÆäËûÒ³ÃæÖ±½ÓÕâÑùÒýÓþÍÐУº
<div th:replace="~{fragments/header
:: header}"></div> |
12¡¢±í´ïʽ»ù±¾¶ÔÏ󣺱í´ïʽ»ù±¾¶ÔÏóÓУºparam¡¢session¡¢application¡¢request¡¢servletContext¡£
Èý¡¢thymeleafÓëspringboot¼¯³É°¸Àý£º
±¾°¸ÀýʹÓÃgradle¹¹½¨£¬Î´Éæ¼°Êý¾Ý¿â£¬Êý¾Ý±£´æÔÚConcurrentMapÖС£Î´ÔøÁ˽âgradleµÄÀÏÌú¿ÉÒԲο¼Ò»ÏÂgradleµÄʹÓᣵãÎÒÏÂÔØ±¾°¸ÀýÔ´Âë¡£ÏîÄ¿½á¹¹ÈçÏ£º

1¡¢Ìí¼ÓÒÀÀµ£º
dependencies
{
compile('org.springframework. boot:spring-boot-starter-web')
testCompile('org.springframework.boot: spring-boot-starter-test'
)
//thymeleafµÄÒÀÀµ
compile
('org.springframework.boot:spring-boot-starter-thymeleaf')
}
|
2¡¢application.properties:
#thymeleafÏà¹ØÅäÖÃ
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5 |
3¡¢entity²ã£º
public class
User {
private Long id;
private Stringname;
private String email;
}
|
4¡¢dao²ã£º
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
concurrent
.
ConcurrentHashMap
;
import
java
.
util
.
concurrent
.
ConcurrentMap
;
import
java
.
util
.
concurrent
.
atomic
.
AtomicLong
;
import
org
.
springframework
.
stereotype
.
Repository
;
import
com
.
zhu
.
test
.
dao
.
UserDao
;
import
com
.
zhu
.
test
.
entity
.
User
;
/**
* user dao²ãʵÏÖ
* @author zhu
*
*/
@Repository
public
class
UserDaoImpl
implements
UserDao
{
//ÓÃÀ´¼ÆÊýµÄ
private
static
AtomicLong
counter
=
new
AtomicLong
();
// ÓÃÀ´±£´æuserµÄmap
private
final
ConcurrentMap
<
Long
,
User
>
userMap
=
new
ConcurrentHashMap
<>();
@Override
public
User
saveOrUpdateUser
(
User
user
)
{
Long
id
=
user
.
getId
();
if
(
id
==
null
)
{
//save
id
=
counter
.
incrementAndGet
();
user
.
setId
(
id
);
}
this
.
userMap
.
put
(
id
,
user
);
return
user
;
}
@Override
public
void
deleteUser
(
Long
id
)
{
this
.
userMap
.
remove
(
id
);
}
@Override
public
User
getUserById
(
Long
id
)
{
return
this
.
userMap
.
get
(
id
);
}
@Override
public
List
<
User
>
listUsers
()
{
return
new
ArrayList
<
User
>(
this
.
userMap
.
values
());
}
} |
½«user±£´æÔÚConcurrentMapÖУ¬crud²Ù×÷Æäʵ¶¼ÊǶÔÕâ¸ömap½øÐвÙ×÷¡£
5¡¢controller²ã£º
@RestController
@RequestMapping
(
"/users"
)
public
class
UserController
{
@Autowired
private
UserDao
userDao
;
/**
* ²éѯËùÓÐÓû§
*
* @param model
* @return
*/
@GetMapping
public
ModelAndView
list
(
Model
model
)
{
model
.
addAttribute
(
"userList"
,
userDao
.
listUsers
());
model
.
addAttribute
(
"title"
,
"Óû§¹ÜÀí"
);
return
new
ModelAndView
(
"user/list"
,
"userModel"
,
model
);
}
/**
* ¸ù¾Ýid²éѯÓû§
*
* @param id
* @param model
* @return
*/
@GetMapping
(
"{id}"
)
public
ModelAndView
view
(
@PathVariable
(
"id"
)
Long
id
,
Model
model
)
{
User
user
=
userDao
.
getUserById
(
id
);
model
.
addAttribute
(
"user"
,
user
);
model
.
addAttribute
(
"title"
,
"²é¿´Óû§"
);
return
new
ModelAndView
(
"user/view"
,
"userModel"
,
model
);
}
/**
* »ñÈ¡´´½¨±íµ¥Ò³Ãæ
*
* @param model
* @return
*/
@GetMapping
(
"/form"
)
public
ModelAndView
createForm
(
Model
model
)
{
model
.
addAttribute
(
"user"
,
new
User
());
model
.
addAttribute
(
"title"
,
"´´½¨Óû§"
);
return
new
ModelAndView
(
"user/form"
,
"userModel"
,
model
);
}
/**
* ±£´æ»ò¸üÐÂÓû§
*
* @param user
* @return
*/
@PostMapping
public
ModelAndView
saveOrUpdateUser
(
User
user
)
{
user
=
userDao
.
saveOrUpdateUser
(
user
);
return
new
ModelAndView
(
"redirect:/users"
);
}
/**
* ɾ³ýÓû§
*
* @param id
* @return
*/
@GetMapping
(
"/delete/{id}"
)
public
ModelAndView
delete
(
@PathVariable
(
"id"
)
Long
id
)
{
userDao
.
deleteUser
(
id
);
return
new
ModelAndView
(
"redirect:/users"
);
// ÖØ¶¨Ïòµ½listÒ³Ãæ
}
/**
* »ñÈ¡ÐÞ¸ÄÓû§µÄ½çÃæ
*
* @param id
* @param model
* @return
*/
@GetMapping
(
"/modify/{id}"
)
public
ModelAndView
modify
(
@PathVariable
(
"id"
)
Long
id
,
Model
model
)
{
User
user
=
userDao
.
getUserById
(
id
);
model
.
addAttribute
(
"user"
,
user
);
model
.
addAttribute
(
"title"
,
"ÐÞ¸ÄÓû§"
);
return
new
ModelAndView
(
"user/form"
,
"userModel"
,
model
);
}
} |
6¡¢Ç°¶ËÒ³Ãæ£º×¢Ò⣺ҪʹÓÃthymeleaf£¬ÐèÒªÔÚhtml±êÇ©ÖмÓÉÏ
xmlns
:
th
=
"http://www.thymeleaf.org"
xmlns
:
layout
=
"http://www.ultraq.net.nz/thymeleaf/layout" |
ÈçÏÂÒ³Ãæ£ºÒ³Í·£ºheader.html
<!DOCTYPE
html>
<html
xmlns:th
=
"http://www.thymeleaf.org"
xmlns:layout
=
"http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta
charset
=
"UTF-8"
>
<title>
thymeleaf in action
</title>
</head>
<body>
<div
th:fragment
=
"header"
>
<h1>
Thymeleaf in action
</h1>
<a
href
=
"/users"
>
Ê×Ò³
</a>
</div>
</body>
</html> |
Ò³½Å£ºfooter.html
<!DOCTYPE
html>
<html
xmlns:th
=
"http://www.thymeleaf.org"
xmlns:layout
=
"http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta
charset
=
"UTF-8"
>
<title>
thymeleaf in action
</title>
</head>
<body>
<div
th:fragment
=
"footer"
>
<a
href
=
"#"
>
ÓÊÏä
</a>
</div>
</body>
</html> |
form.html:
<!DOCTYPE
html>
<html
xmlns:th
=
"http://www.thymeleaf.org"
xmlns:layout
=
"http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta
charset
=
"UTF-8"
>
<title>
thymeleaf in action
</title>
</head>
<body>
<div
th:replace
=
"~{fragments/header :: header}"
></div>
<h3
th:text
=
"${userModel.title}"
>
test
</h3>
<form
action
=
"/users"
th:action
=
"@{/users}"
method
=
"POST"
th:object
=
"${userModel.user}"
>
<input
type
=
"hidden"
name
=
"id"
th:value
=
"*{id}"
>
Ãû³Æ£º
<br>
<input
type
=
"text"
name
=
"name"
th:value
=
"*{name}"
><br>
ÓÊÏ䣺
<br>
<input
type
=
"text"
name
=
"email"
th:value
=
"*{email}"
>
<input
type
=
"submit"
value
=
"Ìá½»"
>
</form>
<div
th:replace
=
"~{fragments/footer :: footer}"
></div>
</body>
</html>
|
list.html:
<!DOCTYPE
html>
<html
xmlns:th
=
"http://www.thymeleaf.org"
xmlns:layout
=
"http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta
charset
=
"UTF-8"
>
<title>
thymeleaf in action
</title>
</head>
<body>
<!-- ÒýÓÃÍ·²¿ÐÅÏ¢ -->
<!-- ÔÚfragmentsϵÄheaderÎļþÏÂÓÐÃûΪheaderµÄƬ¶Î -->
<div
th:replace
=
"~{fragments/header :: header}"
></div>
<h3
th:text
=
"${userModel.title}"
></h3>
<div>
<a
href
=
"/users/form.html"
th:href
=
"@{/users/form}"
>
´´½¨Óû§
</a>
</div>
<table
border
=
"1"
>
<thead>
<tr>
<td>
ID
</td>
<td>
Email
</td>
<td>
Name
</td>
</tr>
</thead>
<tbody>
<tr
th:if
=
"${userModel.userList.size()} eq 0"
>
<td
colspan
=
"3"
>
ûÓÐÓû§ÐÅÏ¢
</td>
</tr>
<tr
th:each
=
"user : ${userModel.userList}"
>
<td
th:text
=
"${user.id}"
></td>
<td
th:text
=
"${user.email}"
></td>
<td
><a
th:href
=
"@{'/users/'+${user.id}}"
th:text
=
"${user.name}"
></a></td>
</tr>
</tbody>
</table>
<div
th:replace
=
"~{fragments/footer :: footer}"
></div>
</body>
</html> |
view.html:
<!DOCTYPE
html>
<html
xmlns:th
=
"http://www.thymeleaf.org"
xmlns:layout
=
"http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta
charset
=
"UTF-8"
>
<title>
thymeleaf in action
</title>
</head>
<body>
<div
th:replace
=
"~{fragments/header :: header}"
></div>
<h3
th:text
=
"${userModel.title}"
>
test
</h3>
<div>
<p><strong>
ID:
</strong><span
th:text
=
"${userModel.user.id}"
></span></p>
<p><strong>
Name:
</strong><span
th:text
=
"${userModel.user.name}"
></span></p>
<p><strong>
Email:
</strong><span
th:text
=
"${userModel.user.email}"
></span></p>
</div>
<div>
<a
th:href
=
"@{'/users/delete/'+${userModel.user.id}}"
>
ɾ³ý
</a>
<a
th:href
=
"@{'/users/modify/'+${userModel.user.id}}"
>
ÐÞ¸Ä
</a>
</div>
<div
th:replace
=
"~{fragments/footer :: footer}"
></div>
</body>
</html> |
ÒÔÉÏÒ³Ãæ¾ÍÉæ¼°µ½ÁËthymeleafµÄ³£ÓñêÇ©£¬Í¨¹ýÕ⼸¸öÒ³Ãæ£¬Àí½âthymeleafµÄÓ÷¨¡£
7¡¢²âÊÔЧ¹û£º

µã»÷¡°´´½¨Óû§¡±£º

µã»÷¡°Ìá½»¡±ºó£º

µã»÷nameÀ¸¿ÉÒÔ½øÈëviewÒ³Ãæ£º

×ܽ᣺
thymeleaf±êÇ©¿´ÆðÀ´ºÜ¶à£¬Æäʵ³£ÓõÄÒ²²»¶à£¬ÇҺܺÃÀí½â¡£ Ö÷Òª±ðÍüÁËÔÚhtml±êÇ©ÖÐÐèÒª¼ÓÉÏ
xmlns:th= "http://www.thymeleaf.org"xmlns:layout=
"http://www.ultraq.net.nz/thymeleaf/layout"¡£
Èç¹ûeclipseдthymeleaf±êǩʱûÓÐÌáʾ£¬°²×°Ò»ÏÂthymeleaf²å¼þÖØÆôeclipse¼´¿É£¬
µã»÷ help-->installnewsoftware£¬µØÖ·Îª£º http://www.thymeleaf.org/eclipse-plugin-update-site/.
|