如何在百万富翁中访问spring会话bean范围

时间:2016-12-29 12:58:15

标签: spring-mvc spring-boot thymeleaf

我已经定义了我的对象

matrix = (Requirement)agent;
Iterator<Object> reqIter = matrix.getRequirements(); //iterate the rows
while (reqIter.hasNext())
{
   Object current = reqIter.next();
   enter.take( new MyAgent(current) ); //PUSH current in the top flow
}

Iterator<Object> sellIter = matrix.getRequirements(); //iterate the columns
while (sellIter.hasNext())
{
   Object current = sellIter.next();
   enter1.take( new MyAgent(current) );   //PUSH current in the bottom flow
}

当我试图从百里香进入时它失败了。

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

     // getter setter
  }

通过spring beans访问

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

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

2 个答案:

答案 0 :(得分:0)

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

您可以访问直接会话属性。

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

答案 1 :(得分:0)

例如会话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>