如何访问 thymeleaf 中的 spring session bean 范围 [英] How to access spring session bean scope in thymeleaf

查看:36
本文介绍了如何访问 thymeleaf 中的 spring session bean 范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了我的对象

  @Component
  @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
  public class MySession {
      private String message;

     // getter setter
  }

当我尝试从 thymeleaf 访问时失败了.

When I try to access from thymeleaf it failed.

<p th:text="${mySession.message}"></p>

解决方案

通过spring bean访问

Accessing through spring beans

http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

 <p th:text="${@mySession.getMessage()}"></p>

推荐答案

例如会话 bean

@Component
@SessionScope
public class State implements Serializable {

    private String pdfPropertyName;

    public String getPdfPropertyName() {
        return pdfPropertyName;
    }
    public void setPdfPropertyName(String pdfPropertyName) {
        this.pdfPropertyName = pdfPropertyName;
    }
}

在控制器中

@Controller
@RequestMapping("uploadPdf")
public class UploadPdfController {

    @Autowired State state;

    @ModelAttribute("pdfPropertyName")
    public String getPdfPropertyName() {
        return state.getPdfPropertyName();
    }

}

可以通过

<span th:text="${pdfPropertyName}"></span>

这篇关于如何访问 thymeleaf 中的 spring session bean 范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆