Question

I have seen similar question but I don't think it's a repost, because I CAN access the ManagedBean from the WebFilter, but strangely the updated property.... isn't really updated. Here are the details:

I have a Filter that does that (using Mojarra 2.1.6 on Glassfish 3.1.2, WebFilter annotation):

public void doFilter(ServletRequest request, ServletResponse response,
              FilterChain chain)
       throws IOException, ServletException {
    // FacesContextHelper is a utility class to get some Faces stuff
    FacesContextHelper facesContextHelper = new FacesContextHelper();
    // this returns the correct instance of a SessionScoped ManagedBean
    userInfoView = (UserInfoView) facesContextHelper.getManagedBean(request,
       response, "userInfoView", UserInfoView.class);
    // ... some computation here to get the 'vm' URL parameter
    userInfoView.setViewMode(viewMode);
    // ....
    chain.doFilter(request, response);
}

Now in a Facelet, I am using this viewMode:

<h:panelGroup layout="block" rendered="#{userInfoView.viewMode ne 1}">
  <ui:include src="/WEB-INF/jsf/v2/products.xhtml"/>
</h:panelGroup>

For the sake of completeness, here is a partial UserInfoView:

@ManagedBean(name = "userInfoView")
@SessionScoped
public class UserInfoView extends AbstractView
   implements ServiceConstant, Serializable {
 private Integer viewMode = 0;

 public Integer getViewMode() {
   return viewMode;
 }

 public void setViewMode(Integer viewMode) {
   this.viewMode = viewMode;
 }
}

Now what happens is that for some reason (it seems linked to the JSESSIONID) I will have the expected behavior:
- having the "vm=1" in the GET request sets (call the UserInfoView setViewMode) the viewMode to 1
- the Facelet gets the appropriate value

Then simply restarting Glassfish and:
- having the "vm=1" in the GET request STILL sets (call the UserInfoView setViewMode) the viewMode to 1
- the Facelet gets the --default** value (i.e. 0)

I suspect that the RESTORE_VIEW phase somehow resets the variables to the default, though I don't have anything to support this theory other than tracing the lifecycle of the requests...

Is there something I should be aware of when changing a variable of a SessionScoped managed bean from a Servlet Filter? Anything wrong in my approach?

Was it helpful?

Solution

Not the answer to your problem, but since you are using JSF 2, a better way to pass those parameters is using:

<f:viewParam name="vm" value="userInfoView.viewMode"/> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top