Session lost in Google App Engine using JSF

╄→гoц情女王★ 提交于 2019-11-29 15:38:36

This is a common problem. What you need to do is force session serialization. This can be done by doing the following:

  • Create a Phase Listener
  • At the end of each phase, store a random attribute on to the session map
    • e.g. sessionMap.put("CURRENT_TIME", System.currentTimeMillis())
  • This will cause the modified data to be serialized to the datastore

The reason you need to do something like this is because, when the view tree was constructed, it was added to the session ... and then your business logic made changes to the components in the view tree, but unfortunately the changes made to these variable do not raise any events that inform GAE to serialize again. Which is why you would see ViewExpiredExceptions or data not being stored, etc.

This concept is similar in nature to the markDirty() concept that you might have come across with other view technologies.

Based on my understanding of Harsha's answer, I post the solution I have used in case of interest for anybody else.

In GaeSession.java:

public class GaeSession implements PhaseListener {
    private static final long serialVersionUID = 1L;

    @Override
    public void afterPhase(PhaseEvent arg0) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("CURRENT_TIME", System.currentTimeMillis());
    }

    @Override
    public void beforePhase(PhaseEvent arg0) {
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    }

}

And in faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <lifecycle>
        <phase-listener>package.GaeSession</phase-listener>
    </lifecycle>

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