thymeleaf實(shí)現(xiàn)前后端數(shù)據(jù)交換的示例詳解
Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 內(nèi)容的模板引擎。它與 JSP,Velocity,F(xiàn)reeMaker 等模板引擎類似,也可以輕易地與 Spring MVC 等 Web 框架集成。與其它模板引擎相比,Thymeleaf 最大的特點(diǎn)是,即使不啟動 Web 應(yīng)用,也可以直接在瀏覽器中打開并正確顯示模板頁面 。
1.前端傳數(shù)據(jù)后端接收:
用戶在登錄界面輸入用戶名和密碼傳給后端controller,由后端判斷是否正確!
在html界面中要傳遞的數(shù)據(jù)name命名,通過表單的提交按鈕會傳遞給響應(yīng)的controller,在controller將需要的name接收!
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}"> <input type="password" name="password" class="form-control" th:placeholder="#{login.password}">
在controller中使用@RequestParam來對應(yīng)接收前端要傳遞的參數(shù),此時參數(shù)名嚴(yán)格對應(yīng)html界面中提交的數(shù)據(jù)name名稱!
@RequestMapping("/user/login") public String Login(@RequestParam("username") String username, @RequestParam("password") String password, Model md){ }
此時后端就實(shí)現(xiàn)接收前端傳遞的數(shù)據(jù)
2.后端對數(shù)據(jù)判斷后返回信息給前端:
controller通過上述參數(shù)會接受到html,傳遞的數(shù)據(jù),對數(shù)據(jù)進(jìn)行判斷。并且通過msg將信息傳遞回去。
if(!StringUtils.isEmpty(username)&& "123123".equals(password)){ return "redirect:/main.html"; }else{ md.addAttribute("msg","用戶名或者密碼錯誤!"); return "index"; }
html頁面使用thymeleaf引擎接收并且顯示數(shù)據(jù)在界面!
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
完整的兩個代碼塊如下:
<form class="form-signin" th:action="@{user/login}"> <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72"> <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1> <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p> <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="" > <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="" > <div class="checkbox mb-3"> <label> <input type="checkbox" value="remember-me" th:text="#{login.remember}"> </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">sign in</button> <p class="mt-5 mb-3 text-muted">? 2022-7-8//21:41</p> <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}" rel="external nofollow" >中文</a> <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}" rel="external nofollow" >English</a> </form>
java
@Controller public class LoginController { @RequestMapping("/user/login") public String Login(@RequestParam("username") String username, @RequestParam("password") String password, Model md){ if(!StringUtils.isEmpty(username)&& "123123".equals(password)){ return "redirect:/main.html"; }else{ md.addAttribute("msg","用戶名或者密碼錯誤!"); return "index"; } } }
到此這篇關(guān)于thymeleaf實(shí)現(xiàn)前后端數(shù)據(jù)交換的文章就介紹到這了,更多相關(guān)thymeleaf數(shù)據(jù)交換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成內(nèi)存數(shù)據(jù)庫Sqlite的實(shí)踐
sqlite這樣的內(nèi)存數(shù)據(jù)庫,小巧可愛,做小型服務(wù)端演示程序,非常好用,本文主要介紹了SpringBoot集成Sqlite,具有一定的參考價值,感興趣的可以了解一下2021-09-09spring boot過濾器FilterRegistrationBean實(shí)現(xiàn)方式
這篇文章主要介紹了spring boot過濾器FilterRegistrationBean實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Springboot利用Aop捕捉注解實(shí)現(xiàn)業(yè)務(wù)異步執(zhí)行
在開發(fā)過程中,盡量會將比較耗時且并不會影響請求的響應(yīng)結(jié)果的業(yè)務(wù)放在異步線程池中進(jìn)行處理,那么到時什么任務(wù)在執(zhí)行的時候會創(chuàng)建單獨(dú)的線程進(jìn)行處理呢?這篇文章主要介紹了Springboot利用Aop捕捉注解實(shí)現(xiàn)業(yè)務(wù)異步執(zhí)行2023-04-04mybatis取別名typeAliases標(biāo)簽的位置放錯導(dǎo)致報錯的解決
這篇文章主要介紹了mybatis取別名typeAliases標(biāo)簽的位置放錯導(dǎo)致報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09IDEA代碼規(guī)范&質(zhì)量檢查的實(shí)現(xiàn)
這篇文章主要介紹了IDEA代碼規(guī)范&質(zhì)量檢查的實(shí)現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08springboot+idea熱部署的實(shí)現(xiàn)方法(自動刷新)
這篇文章主要介紹了springboot+idea熱部署的實(shí)現(xiàn)方法(自動刷新),本文分步驟通過實(shí)例代碼截圖相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05