spring-boot - 在 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"
}
我努力了
<tbody th:object="${networker}">
如果我使用
th.text="${@networker.name}"
我没有得到任何内容但编译不会抱怨我尝试了类似
th:object="${#servletContext.getAttribute(networker)}"
但没有一个显示我可以编辑的数据
请让我参考一些工作样本
解决方案
解决方案很简单,我不需要
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
我只需要注释我@Controller
的 with@SessionAttributes("networker")
和来自 HTML 的调用即可th:object="${networker}"
完美运行
推荐阅读
- android - 尝试在 android 中实现电子邮件意图
- swift - 如何快速正确解码模型
- java - 在mac中卸载openjdk
- python - 如何在 IronPython 上找到数组项的交集?
- xslt - 中间没有任何空格的长句子流入相邻的单元格/块:
- json - 如何在 AWS Cloudformation 资源部分的 Fn:Join 中使用用户输入的参数作为参考
- python - 是否可以在字典理解内分配变量?
- amazon-web-services - 将 t3a.medium 转换为 t4g.medium EC2 实例
- node.js - 反应站点网络 main.chunk 长负载
- scala - elastic4s中如何使用search_after