首页 > 解决方案 > 在 Thymeleaf 中访问 @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)

问题描述

我似乎无法从 HTML 中的 Thymeleaf 访问我的 Scoped Proxy Data 类 Singleton

我的数据类

@Document(collection = "networker")
open class Networker(
    @Id
    var id: String? = null,
    @TextIndexed
    @Indexed(direction = IndexDirection.ASCENDING)
    var name: String? = null,
....

然后我的@Configuration豆子

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
    fun networker(): Networker {
        return Networker()
    }

然后我得到正确的记录并添加模型属性

@GetMapping(value = ["index"])
    @ModelAttribute(value = "networker")
    fun profileView(model: Model): String {

        val authentication = SecurityContextHolder.getContext().authentication
        val username: String = authentication.name
        networker = networkerRepository.findBySecurityUserName(username)

        model.addAttribute("networker", networker)
        model.addAttribute("networkerSettings", networkerSettings)

        return "profile/index"
    }

我努力了

  1. <tbody th:object="${networker}">

  2. 如果我使用th.text="${@networker.name}"我没有得到任何内容但编译不会抱怨

  3. 我尝试了类似th:object="${#servletContext.getAttribute(networker)}"但没有一个显示我可以编辑的数据

请让我参考一些工作样本

标签: spring-bootkotlinthymeleaf

解决方案


解决方案很简单,我不需要

@Bean
    @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)

我只需要注释我@Controller的 with@SessionAttributes("networker")和来自 HTML 的调用即可th:object="${networker}"完美运行


推荐阅读