Spring mvc 如何在thymeleaf中访问spring会话bean范围
我已经定义了我的对象Spring mvc 如何在thymeleaf中访问spring会话bean范围,spring-mvc,spring-boot,thymeleaf,Spring Mvc,Spring Boot,Thymeleaf,我已经定义了我的对象 @组件 @范围(value=“session”,proxyMode=ScopedProxyMode.TARGET\u类) 公共班级会议{ 私有字符串消息; //吸气剂设定器 } 当我尝试从thymeleaf访问时,它失败了 解决方案 通过Springbeans访问 您可以访问直接会话属性 ${#session.getAttribute('mySessionAttribute')} 例如会话bean @Component @SessionScope public
@组件
@范围(value=“session”,proxyMode=ScopedProxyMode.TARGET\u类)
公共班级会议{
私有字符串消息;
//吸气剂设定器
}
当我尝试从thymeleaf访问时,它失败了
解决方案
通过Springbeans访问
您可以访问直接会话属性
${#session.getAttribute('mySessionAttribute')}
例如会话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>
什么是失败的?任何错误?错误消息:在null上找不到属性或字段“message”。但是,我找到了解决方案,通过SpringBeans访问
<span th:text="${pdfPropertyName}"></span>