1

I have a little problem: when logging out of my cms, SOMEHOW the setter of one of my parameters is called and action performed. It results in http500.

To be specific: I pass the parameter from jsf page to backing (@RequestScoped) bean using:

<f:metadata>
<f:viewParam name="activate" value="#{myManagedBean.activate}" />

it uses setter, which makes changes in database:

public void setActivate(int num) {
    ab.changeActiveState("myString", num);
    return;
}

logout method is used on completely different bean, and calls simple method:

public String logout() {
    Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
    HttpServletRequest a = (HttpServletRequest) request;                
    try {
        a.logout();
    } catch(Exception e) {
        logger.info("Error while logout: " + e);
    }
    return "advert-list.xhtml?faces-redirect=true";
}

Logging out throws errors:

javax.servlet.ServletException: javax.el.ELException: /secure/my-list.xhtml @12,81 value="#{myManagedBean.activate}": javax.ejb.EJBException: javax.persistence.NoResultException: No entity found for query
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)  org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349) 
root cause
javax.faces.component.UpdateModelException: javax.el.ELException: /secure/advert-list.xhtml @12,81 value="#{myManagedBean.activate}": javax.ejb.EJBException:
javax.persistence.NoResultException: No entity found for query
javax.faces.component.UIInput.updateModel(UIInput.java:848)
javax.faces.component.UIViewParameter.updateModel(UIViewParameter.java:284)
javax.faces.component.UIInput.processUpdates(UIInput.java:730)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1109)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1109)
javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1218)   com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:74)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)  org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)

I have no idea what is going on and why. About that setter - it works as it should. And about logging out - from any other page in my project it doesn't throw anything and logs out current user.

Olek Olkuski
  • 83
  • 2
  • 8

2 Answers2

0

Like as with getter methods, you shouldn't be doing business logic in setter methods at all.

If you intend to perform actions on an initial request after the <f:viewParam> has done its job, then you should be using a <f:event type="preRenderView"> listener method.

<f:metadata>
    <f:viewParam name="activate" value="#{myManagedBean.activate}" />
    <f:event type="preRenderView" listener="#{myManagedBean.init}" />
</f:metadata>

with

private int num;

public void init() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        ab.changeActiveState("myString", num);
    }
}

public void setActivate(int num) {
    this.num = num;
}

Note that this is in the upcoming JSF 2.2 to be replaced by <f:viewAction onPostback="false">.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I need to preform action like radio button, but on data from h:dataTable. Activating one row must disable any other active at that time. I pass the argument via f:viewParam. Then the setter invokes action responsible for enabling specified item and disabling others. Will replace the action I've done by using setter? Oh, and apart from changing active row I have other options, like delete and other column where active row can be changed. Will that work together? – Olek Olkuski Jul 01 '12 at 16:35
0

Maybe not the best, but working way to deal with this problem, is to add the if-statement in setter:

if (!FacesContext.getCurrentInstance().isPostback()) {
    ab.changeActiveState("myString", num);
}

If You know any other, more politically-correct way to do it, please, let me know.

By the way - I was trying to pass the parameter in some other way, but none of them works. read something on Mykong page (1) and in comments I found that some of those ways don't work with request scoped beans...

Mykong Page

Olek Olkuski
  • 83
  • 2
  • 8