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?
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.
}
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With