何度か更新するページがあります。そのページの更新で初めて変数を設定したい。ただし、ページが更新されるたびに、この変数が再び設定されます。
この変数が設定する最初の更新時にのみ、何をする必要がありますか?
何度か更新するページがあります。そのページの更新で初めて変数を設定したい。ただし、ページが更新されるたびに、この変数が再び設定されます。
この変数が設定する最初の更新時にのみ、何をする必要がありますか?
その変数をsession parameter
例えば:
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.
}
jsfページのビュータグのbeforePhase属性を次のように設定します。
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
beforePhase="#{pageFlowScope.[YourClass]Bean.beforePhase}">
したがって、たとえば PageFlowScope で Bean を作成し、次のように BeforePhase メソッドを追加する必要があります。
public void beforePhase(PhaseEvent phaseEvent)
{
FacesContext context = FacesContext.getCurrentInstance();
if(!context.getRenderKit().getResponseStateManager().isPostback(context))
{
//Do your thing.. (Set your variable etc.)
}
}
Bean を pageFlowScope に追加したことを確認したら、コードは準備完了です...
これは始めるのに役立ちますか?;)
<%
String yourVar = session.getAttribute( "yourVariable" );
if(yourVar == null || yourVar == ""){
yourVar = request.getParameter("sourceVariable");
session.setAttribute("yourVar", yourVar);
}
%>
This is set: ${yourVar}
もう 1 つの方法は、JSP に行く前であっても、コントローラ側で処理することです。ただし、基本的には同じです。変数が設定されている場合は、セッションを使用して変数を保持します。