1
votes

I am using HttpSession as an instance variable like

@ManagedBean
@ViewScoped
public class CountryPages_Detail {

    private HttpSession session;

    public CountryPages_Detail() {
        session = ConnectionUtil.getCurrentSession();
    } //end of constructor

    public String preview() {
        session.setAttribute("countryDetailImageNames", imageNames);
        session.setAttribute("countryDetailImages", images);
        session.setAttribute("countrySummary", countrySummary);        
        return "countryPages_View?faces-redirect=true";

    } //end of preview()
} //end of  class CountryPages_Detail

ConnectionUtl Class:

 public static HttpSession getCurrentSession() {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExternalContext externalContext = facesContext.getExternalContext();
     HttpServletRequest httpServletRequest = (HttpServletRequest) externalContext.getRequest();

     HttpSession currentSession = (HttpSession) externalContext.getSession(false);

     if (currentSession != null) {         
         return currentSession;
     } else {         
         return null;
     }
} //end of getCurrentSession()

I want to ask is this right way? Actually i was using in my web.xml

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

But when i changed it to <param-value>client</param-value>, then first i got the exception that one of my class is not serializable, after making it serializable now i am getting exception that

SEVERE: Error Rendering View[/index.xhtml]
java.io.NotSerializableException: org.apache.catalina.session.StandardSessionFacade
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    ....

When using server it was running fine. Why? I want to ask when we server in param-valuethen all our managed bean that are in viewScope(@ViewScoped) reside on server, and when we change it to client in then all our @ViewScoped managed bean reside on client? Also if my beans are not in @ViewScoped, then does javax.faces.STATE_SAVING_METHOD element create any difference? Means STATE_SAVING_METHOD option relates to only @ViewScoped or it also effects @RequestScope or @SessionScopr or other scopes? thanks

1

1 Answers

2
votes

You should absolutely not get hold of external context resources like HttpSession as an instance variable. Just retrieve it in the threadlocal scope. You can use ExternalContext#sessionMap() to manage the session attribute map without the need to have javax.servlet imports in your JSF code (which is usually a sign that you're doing things the wrong or clumsy way, whereby you're completely working your way around JSF without utilizing JSF's powers).

public String preview() {
    Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    sessionMap.put("countryDetailImageNames", imageNames);
    sessionMap.put("countryDetailImages", images);
    sessionMap.put("countrySummary", countrySummary);        
    return "countryPages_View?faces-redirect=true";
}

Better, however, is to create a session scoped managed bean. You've there namely some strange and rather procedural and non-OO design.

@ManagedBean
@SessionScoped
public class Country {

     private List<Something> detailImageNames;
     private List<Something> images;
     private Something summary;

     // ...
}

which you use as follows inside CountryPagesDetail:

@ManagedProperty("#{country}")
private Country country;

public String preview() {
    country.setDetailImageNames(imageNames);
    country.setDetailImages(images);
    country.setSummary(summary);        
    return "countryPages_View?faces-redirect=true";
}

It's available by #{country.detailImageNames} and so on.