问题
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