How to access spring session bean scope in thymeleaf

a 夏天 提交于 2021-02-07 17:27:37

问题


I have define my object

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

     // getter setter
  }

When I try to access from thymeleaf it failed.

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

SOLUTION

Accessing through spring beans

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

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

回答1:


session.setAttribute("mySessionAttribute", "someValue");

You can access direct session attribute .

${#session.getAttribute('mySessionAttribute')}



回答2:


For example the session bean

@Component
@SessionScope
public class State implements Serializable {

    private String pdfPropertyName;

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

In the Controller

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

    @Autowired State state;

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

}

Can be accessed via

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


来源:https://stackoverflow.com/questions/41380241/how-to-access-spring-session-bean-scope-in-thymeleaf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!