user2978972
user2978972

Reputation: 51

Set variable only for first time page refresh from Java bean?

I have a page that refresh several times. I want to set the variable for the first time that page refresh. but every time page refresh this variable sets again.

What do I need to do that only at the first refresh this variable sets?

Upvotes: 5

Views: 1648

Answers (3)

Bedir Yilmaz
Bedir Yilmaz

Reputation: 4083

Set the beforePhase attribute of your jsf Page's view tag like this

<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
        beforePhase="#{pageFlowScope.[YourClass]Bean.beforePhase}">

So you you'll want to create a bean in say, PageFlowScope, and then add a BeforePhase Method like this:

public void beforePhase(PhaseEvent phaseEvent) 
{

    FacesContext context = FacesContext.getCurrentInstance();

    if(!context.getRenderKit().getResponseStateManager().isPostback(context))
    {
     //Do your thing.. (Set your variable etc.)
    }

}

After you make sure that you've added your bean to your pageFlowScope, your code is good to go...

Upvotes: 1

Dropout
Dropout

Reputation: 13866

Does this help you get started? ;)

<%
    String yourVar = session.getAttribute( "yourVariable" );

    if(yourVar == null || yourVar == ""){
        yourVar = request.getParameter("sourceVariable");
        session.setAttribute("yourVar", yourVar);
    }   
%>

This is set: ${yourVar}

Another approach is to handle it on the controller side, even before going to the JSP. However it's basically the same - you use session to hold the variable if it's set.

Upvotes: 1

codeMan
codeMan

Reputation: 5758

make that variable as a session parameter

for example:

HttpSession session = request.getSession(false);//will give a session object only if it exists
int refreshcount =0;
if(session == null)//which means its the first request to the page
{
    refreshcount++;
}
else
{
    //do nothing.
}

Upvotes: 3

Related Questions