最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

thymeleaf實現(xiàn)前后端數(shù)據(jù)交換的示例詳解

 更新時間:2022年07月09日 14:45:26   作者:lumanman  
Thymeleaf?是一款用于渲染?XML/XHTML/HTML5?內(nèi)容的模板引擎,當通過?Web?應(yīng)用程序訪問時,Thymeleaf?會動態(tài)地替換掉靜態(tài)內(nèi)容,使頁面動態(tài)顯示,這篇文章主要介紹了thymeleaf實現(xiàn)前后端數(shù)據(jù)交換,需要的朋友可以參考下

Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 內(nèi)容的模板引擎。它與 JSP,Velocity,F(xiàn)reeMaker 等模板引擎類似,也可以輕易地與 Spring MVC 等 Web 框架集成。與其它模板引擎相比,Thymeleaf 最大的特點是,即使不啟動 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īng)html界面中提交的數(shù)據(jù)name名稱!

@RequestMapping("/user/login")
 public String Login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model md){      
        }

此時后端就實現(xiàn)接收前端傳遞的數(shù)據(jù)

2.后端對數(shù)據(jù)判斷后返回信息給前端:

controller通過上述參數(shù)會接受到html,傳遞的數(shù)據(jù),對數(shù)據(jù)進行判斷。并且通過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實現(xiàn)前后端數(shù)據(jù)交換的文章就介紹到這了,更多相關(guān)thymeleaf數(shù)據(jù)交換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

康马县| 阜康市| 康定县| 奈曼旗| 民丰县| 安远县| 陆川县| 炉霍县| 工布江达县| 聂荣县| 庆阳市| 烟台市| 安福县| 长岛县| 开化县| 高唐县| 边坝县| 钟山县| 府谷县| 北票市| 沁阳市| 织金县| 通江县| 蓝田县| 金门县| 白沙| 克什克腾旗| 元阳县| 深泽县| 杭锦后旗| 绩溪县| 佛教| 寿阳县| 宣化县| 武冈市| 汝阳县| 广汉市| 安陆市| 连江县| 武安市| 庆安县|