Controller层实现页面跳转

此篇博客详细介绍了如何在Spring MVC的Controller中实现页面跳转,并通过HttpSession的invalidate()方法销毁会话,重点在于使用redirect:指定的URL重定向到登录页面的操作。

Controller层实现页面跳转

 @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request) {

        //销毁session
        HttpSession session = request.getSession();

        session.invalidate();//销毁

        //回到登入页
        return "redirect:/users/loginUi";
    }

return “redirect:/users/loginUi”;
redirect:后加“/”,可以返回绝对路径,十分好用,之前一直没加“/”,返回的都很奇怪。

在Java Web开发中,特别是在使用Spring MVC框架时,要在Controller实现页面跳转前显示确认弹框,你可以这样做: 1. 首先,创建一个自定义的`Thymeleaf`模板或者HTML视图,比如`confirm-dialog.html`。在这个模板中,添加一个表单或者按钮,包含一个JavaScript确认函数,例如: ```html <!DOCTYPE html> <html th:fragment="confirmDialog"> <script type="text/javascript"> function confirmRedirect() { if (confirm("是否确定要进行操作?")) { return true; } else { return false; } } </script> <form id="confirmForm" action="#" th:action="@{/your-target-url}" th:object="${yourModel}"> <button type="submit" onclick="return confirmRedirect()">确认</button> <a href="#" onclick="event.preventDefault();">取消</a> </form> </html> ``` 2. 在需要确认的Controller方法中,使用` ThymeleafTemplateEngine`来渲染这个模板,并将其结果返回给前端。同时,将表单的提交事件绑定到JavaScript函数上: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class YourController { @GetMapping("/confirm") public String confirmAction(Model model) { // 渲染确认对话框 model.addAttribute("yourModel", new YourModelObject()); return "confirm-dialog"; } @PostMapping("/confirm") public String confirmPost(@ModelAttribute("yourModel") YourModelObject model) { // 用户确认后,处理逻辑并跳转到目标URL if (/* 检查用户的确认 */ true) { return "redirect:/your-target-url"; } else { // 取消则返回之前的页面或者默认页面 return "redirect:/default-page"; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值