0

I have 2 jsf pages, each of them have a managed bean. I'm navigating out of the second page to the first one , and want to destroy the entity object inside of it. my problem is that after i set it to null , It still going into the getDetails method which creates a new entity..

how can I prevent it from going to the getDetails method when exiting this page? how can I destroy this entity correctly? am I doing somthing wrong?

This is my code:

public class page2MB {

@EJB
private page2SessionBean page2SessionBean;
private Page2 page2;

public page2MB() {
}

public Page2 getDetails()
{
    if(page2 == null)
    {
        Map requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();   
        page2 = new Page2();  
        page2.setPage2PK(new Page2PK(Short.parseShort((String)requestParams.get("param1")),
                                          Short.parseShort((String)requestParams.get("param2"))));            

        page2 = page2SessionBean.find(page2);    
    }        
    return page2;
}

public String exit() 
{       
    try
    {
        page2 = null;
        return "page1";
    }
    catch(Exception exp)
    {                      
        exp.printStackTrace();
        return "";
    } 
    finally
    {

    }
}    
}

page2.xhtml:

<f:view>
       <h:form>                              
            <h:panelGrid columns="2">                
                <h:inputText id="page2PKfield1" value="#{page2MB.details.page2PK.field1}"/>                                                                   
                <h:inputText id="page1MBfield1" value="#{page1MB.details.field1}"/>                                                   
                <h:inputText id="page2MBfield2" value="#{page2MB.details.page2PK.field2}"/>                                            
                <h:inputText id="field2Desc" value="#{page2MB.details.field2Desc}"/>                    
            </h:panelGrid>                                
            <h:commandButton id="exit"   value="יציאה" action="#{page2MB.exit}" immediate="true"></h:commandButton>                                     
        </h:form>
    </f:view>

Thank's In Advance.

user590586
  • 2,960
  • 16
  • 63
  • 96

1 Answers1

2

beacuse it has to be session scoped. I have more actions on this page which requires to be in session scoped mode.

Don't mix data which belongs in different scopes in a single bean in the broadest possible scope. Create separate beans in different scopes which are proper for the data the bean holds and inject the one bean in the other bean using @ManagedProperty.

@ManagedBean
@SessionScoped
public class SessionBean {

    private Object sessionData;

    // ...
}
@ManagedBean
@ViewScoped
public class ViewBean {

    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;

    private Object viewData;

    // ...
}

Also don't do the business job in getters. Rather do it in the @PostConstruct method.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank's for the answer, I've chaned the bean to `view scoped`, and added the `@PostConstruct` and also started using the `@ManagedProperty`. I just have one more problem - when I'm navigating to the page2.xhtml (by using return "page2") I see page2 with the correct values but it is not rtl like it's supposed to be and also I can't view it's source. what am I missing? – user590586 Jul 31 '12 at 14:19