5

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?

pnuts
  • 58,317
  • 11
  • 87
  • 139

3 Answers3

3

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.
}
codeMan
  • 5,730
  • 3
  • 27
  • 51
1

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.

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • where from do you set the variable in the first load? – Dropout Nov 11 '13 at 12:27
  • aha.. then my answer won't help you.. try reading the session variable in your constructor then.. if it isn't set then set it, otherwise do nothing.. it's the same logic, just in a different place.. – Dropout Nov 11 '13 at 12:38
1

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...

Bedir Yilmaz
  • 3,823
  • 5
  • 34
  • 54