Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 864
user2978972 Avatar asked Nov 11 '13 11:11

user2978972


3 Answers

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.
}
like image 96
codeMan Avatar answered Nov 15 '22 07:11

codeMan


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.

like image 44
Dropout Avatar answered Nov 15 '22 06:11

Dropout


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

like image 1
Bedir Yilmaz Avatar answered Nov 15 '22 07:11

Bedir Yilmaz