springboot:使用thymeleaf实现html前端页面和controller控制层数据交换

1.前端传数据后端接收:

用户在登录界面输入用户名和密码传给后端controller,由后端判断是否正确!

在html界面中要传递的数据name命名,通过表单的提交按钮会传递给响应的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来对应接收前端要传递的参数,此时参数名严格对应html界面中提交的数据name名称!

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

此时后端就实现接收前端传递的数据

2.后端对数据判断后返回信息给前端:

controller通过上述参数会接受到html,传递的数据,对数据进行判断。并且通过msg将信息传递回去。

if(!StringUtils.isEmpty(username)&& "123123".equals(password)){
            return "redirect:/main.html";
        }else{
            md.addAttribute("msg","用户名或者密码错误!");
            return "index";
        }

html页面使用thymeleaf引擎接收并且显示数据在界面!

<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')}">中文</a>
			<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">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";
        }

    }
}

在这里插入图片描述

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现前端页面分页,可以按照以下步骤进行: 1. 在前端页面中添加分页组件,例如bootstrap的分页组件,或者自己写一个分页组件。 2. 在后端接口中,通过mybatis查询数据库中的数据,并将查询到的数据返回给前端。 3. 在后端接口中,通过springboot的PageHelper插件对查询结果进行分页处理,得到分页后的数据。 4. 将分页后的数据返回给前端,并在前端页面中显示分页数据。 5. 在前端页面中,监听分页组件的事件,例如翻页、改变每页显示条数等事件,将事件参数传递给后端接口,重新查询并分页处理数据,更新前端页面显示的数据。 下面是一个使用thymeleaf模板引擎的示例代码: Controller代码: ```java @GetMapping("/list") public String list(Model model, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userService.getUserList(); PageInfo<User> pageInfo = new PageInfo<User>(userList); model.addAttribute("pageInfo", pageInfo); return "user/list"; } ``` Thymeleaf页面代码: ```html <div class="container"> <table class="table table-striped"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>邮箱</th> </tr> </thead> <tbody> <tr th:each="user : ${pageInfo.list}"> <td th:text="${user.id}"></td> <td th:text="${user.username}"></td> <td th:text="${user.email}"></td> </tr> </tbody> </table> <ul class="pagination"> <li th:class="${pageInfo.isFirstPage() ? 'disabled' : ''}"> <a th:href="@{${#httpServletRequest.requestURI}(pageNum=1,pageSize=${pageInfo.pageSize})}">«</a> </li> <li th:class="${pageInfo.isFirstPage() ? 'disabled' : ''}"> <a th:href="@{${#httpServletRequest.requestURI}(pageNum=${pageInfo.prePage},pageSize=${pageInfo.pageSize})}">‹</a> </li> <li th:each="page : ${pageInfo.navigatepageNums}" th:class="${page == pageInfo.pageNum ? 'active' : ''}"> <a th:href="@{${#httpServletRequest.requestURI}(pageNum=${page},pageSize=${pageInfo.pageSize})}" th:text="${page}"></a> </li> <li th:class="${pageInfo.isLastPage() ? 'disabled' : ''}"> <a th:href="@{${#httpServletRequest.requestURI}(pageNum=${pageInfo.nextPage},pageSize=${pageInfo.pageSize})}">›</a> </li> <li th:class="${pageInfo.isLastPage() ? 'disabled' : ''}"> <a th:href="@{${#httpServletRequest.requestURI}(pageNum=${pageInfo.pages},pageSize=${pageInfo.pageSize})}">»</a> </li> </ul> </div> ``` 在上述代码中,使用了PageHelper插件对查询结果进行分页处理,并将分页信息放入PageInfo对象中,然后将PageInfo对象放入Model中,传递给Thymeleaf模板引擎进行渲染。在Thymeleaf页面中,使用bootstrap的分页组件,根据PageInfo对象中的分页信息动态生成分页按钮,并监听按钮的点击事件,通过URL传递参数给后端接口重新查询并分页处理数据,更新前端页面显示的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值