首页 > 其他分享 >使用springboot+thymeleaf 在html中获取session

使用springboot+thymeleaf 在html中获取session

时间:2024-04-29 20:35:55浏览次数:17  
标签:springboot Controller html thymeleaf session loginUserId error import String

Controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @GetMapping("/your-page")
    public String yourPage(HttpSession session, Model model) {
        // 获取session中的值
        String loginUserId = (String) session.getAttribute("loginUserId");
        
        // 将值传递给视图
        model.addAttribute("loginUserId", loginUserId);
        
        // 返回视图名称
        return "your-page"; // 这里的 "your-page" 是你的Thymeleaf模板文件的名称
    }
}

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Page</title>
    <!-- 引入jQuery等必要的JavaScript库 -->
</head>
<body>
    <script th:inline="javascript">
        $(document).ready(function() {
            var loginUserId = /*[[${loginUserId}]]*/ '';
            $.ajax({
                url: 'admin/getUser?adminId=' + loginUserId,
                type: 'GET',
                dataType: 'json',
                success: function(data) {
                    // 处理获取的用户数据
                },
                error: function(xhr, status, error) {
                    console.error('Error fetching user data:', error);
                }
            });
        });
    </script>
</body>
</html>

标签:springboot,Controller,html,thymeleaf,session,loginUserId,error,import,String
From: https://www.cnblogs.com/java-six/p/18166600

相关文章

  • springboot连接sqlserver2008r2 驱动版本问题。
    <dependency><groupId>com.microsoft.sqlserver</groupId><artifactId>mssql-jdbc</artifactId><version>8.2.2.jre8</version></dependency>版本比较常见的:8.4.1.jre8最新的提示:9......
  • html-docx-js 导出word
    1:列表页面按钮<el-buttontype="warning"plainicon="el-icon-download"size="mini"@click="exportWorddata">导出word</el-button> <......
  • SpringBoot配置HTTPS及开发调试
    前言在实际开发过程中,如果后端需要启用https访问,通常项目启动后配置nginx代理再配置https,前端调用时高版本的chrome还会因为证书未信任导致调用失败,通过摸索整理一套开发调试下的https方案,特此分享后端配置生成HTTPS密钥keytool-genkeypair-aliastomcat-keyalgRSA-keysi......
  • IDEA导入springboot项目无法识别resources下的application.yml配置文件
    遇到的问题:IDEA springboot不能读取resources下的yml配置文件如下图,application.yml文件前面的图标并不是springboot配置文件的图标,这就是IDEA没有识别到yml文件 正确的配置文件图标因该是下面这样的可能原因及解决方法: 文件名问题:确保YML文件的名称是正确......
  • 使用idea 在线创建springboot 项目-需联网
    1.打开idea,点击File,New,Project...2.配置项目名称和地址,配置jdk版本.配置完成点击Next等待导入依赖没有jdk17就下载一下想要在resources目录创建.yml结尾文件,按步骤操作选择Editor->FileandCodeTemplates选择files点击+号,输入名称,和文件类型.......
  • 实现HTML标签的转义、反转义的几种方法
    原文链接:https://blog.csdn.net/huanggang0101/article/details/99621029方法一:通过正则表达式进行替换1,HTML标签的转义方法//HTML标签转义(<-----><)functionhtml2Escape(sHtml){returnsHtml.replace(/[<>&"]/g,function(c){return{'<':�......
  • day27-HTML
    1.web开发1.1、最简单的web应用程序importsocketsock=socket.socket()sock.bind(("127.0.0.1",8800))sock.listen(5)while1:print("serverisworking...")conn,addr=sock.accept()recv_data=conn.recv(1024)conn.send(b"HTTP/1.......
  • HTML转义字符
    原文链接:https://blog.csdn.net/fengxing11/article/details/39827753HTML中<,>,&等有特别含义,(前两个字符用于链接签,&用于转义),不能直接使用。使用这三个字符时,应使用他们的转义序列。转义字符串(EscapeSequence)也称字符实体(CharacterEntity)。在HTML中,定义转义字符串的原因......
  • HTML一键打包APK工具一机一码功能更新 - 增强安全性
    HTML一键打包APK工具在上一个版本更新的时候,添加了一机一码功能,该功能上线以来,受到许多用户的喜爱。它可以方便的让APK只能在已经授权的手机上使用,能有效地保护APK作品,防止一些没有授权的传播。如果想要向用户收费或者限制使用范围,这个功能就非常适合。之前版本中,在HTML一键打包A......
  • SpringBoot自动装配原理
     个人理解,SpringBoot就是由Spring和SpringMVC整合而来。开箱即用、简化监控、简化配置、简化部署;约定大于配置;boot脱离了传统Spring手动配置大量的bean,而SpringBoot自动装配改变了繁琐的局面。 SpringBoot的自动配置基于条件注解和自动配置类,它能够根据应用程序的......