開發傳統Java WEB工程時,我們可以使用JSP頁面模板語言,但是在SpringBoot中已經不推薦使用了。SpringBoot支持如下頁面模板語言
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- JSP
其中Thymeleaf是SpringBoot官方所推薦使用的,下面來談談Thymeleaf一些常用的語法規則。
@ModelAttribute
@Controller
public class FormatterController
{
@RequestMapping(value = "/getForm", method = RequestMethod.POST)
public String test(@ModelAttribute User user,Model model)
{
model.addAttribute("user", user);
return "success";
}
//Cjsp
@Controller
@RequestMapping(value = "/user")
public class FormatterController
{
@RequestMapping(value = "/getForm", method = RequestMethod.POST)
public String test(@ModelAttribute User user,Model model)
{
model.addAttribute("user", user);
return "success";
}
//V側
<form:form modelAttribute="user" method="post" action="getForm">
<form:input path="birthday"/>
<form role="form" action="/simple/confirm" th:action="@{/simple/confirm}" th:object="${simpleForm}" method="post">
資源文件中定義了內容welcome.msg=歡迎{0}光臨!。可以這樣子在頁面中將其顯示
<h2 th:text="#{welcome.msg('admin')}"/>
th:utext和th:text的區別是:
th:text會對<和>進行轉義,
而th:utext不會轉義。
關於“${屬性}”和“{屬性}”的區別?
${}訪問完整信息,而{}訪問指定對象中的屬性內容, 如果訪問的只是普通的內容兩者沒有區別;
<div>
<p th:text="'用戶編號:' + ${member.uid}"/>
<p th:text="'用戶姓名:' + ${member.name}"/>
<p th:text="'用戶年齡:' + ${member.age}"/>
<p th:text="'用戶工資:' + ${member.salary}"/>
<p th:text="'出生日期:' + ${member.birthday}"/>
<p th:text="'出生日期:' + ${#dates.format(member.birthday,'yyyy-MM-dd')}"/>
</div>
<hr/>
<div th:object="${member}">
<p th:text="'用戶編號:' + *{uid}"/>
<p th:text="'用戶姓名:' + *{name}"/>
<p th:text="'用戶年齡:' + *{age}"/>
<p th:text="'用戶工資:' + *{salary}"/>
<p th:text="'出生日期:' + *{birthday}"/>
<p th:text="'出生日期:' + *{#dates.format(birthday,'yyyy-MM-dd')}"/>
</div>
//各種controller方法例子
@RequestMapping(value="/result", method=RequestMethod.POST)
public String indexFormSubmit(@ModelAttribute IndexForm indexForm, Model model) {
if (indexForm.getId() == 1) {
indexForm.setContent("お前がナンバーワンだ!");
}
model.addAttribute("indexForm", indexForm);
return "result";
}
//提交的form數據從indexForm取得, 返回數據用model
推薦★★★ 項目詳解
qiita.com/opengl-8080/items/05d9490d6f0544e2351a
項目目錄 qiita.com/NariseT/items/172ca093364aa9391989
spring-boot-example1
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── spring-boot-example1.iml
└── src
├── main
│ ├── java
│ │ └── org
│ │ └── mygroup
│ │ └── webapp
│ │ └── SpringBootExample1Application.java
│ └── resources
│ └── application.properties
│ └── templates
│ └── index.html
└── test
└── java
└── org
└── mygroup
└── webapp
└── SpringBootExample1ApplicationTests.java
參考
www.jianshu.com/p/a842e5b5012e
SpringBoot Thymeleaf頁面顯示語法
qiita.com/NariseT/items/172ca093364aa9391989
項目目錄
itsakura.com/java-springboot-submit
Java Spring Boot form 傳值的例子 參數以REST形式取得的時候。
qiita.com/opengl-8080/items/05d9490d6f0544e2351a
項目詳解
srping boot annotation 註解一覽
qiita.com/TEBASAKI/items/267c261db17f178e33eb
連載 Spring Boot解説第18回