是否仅为首次从JavaBean刷新页面设置变量?
我有一个页面可以刷新几次。我想在第一次刷新页面时设置变量。但每次页面刷新时,该变量都会再次设置是否仅为首次从JavaBean刷新页面设置变量?,java,jsp,oracle-adf,jdeveloper,Java,Jsp,Oracle Adf,Jdeveloper,我有一个页面可以刷新几次。我想在第一次刷新页面时设置变量。但每次页面刷新时,该变量都会再次设置 仅在第一次刷新此变量集时,我需要执行哪些操作?将该变量作为会话参数 例如: 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 r
仅在第一次刷新此变量集时,我需要执行哪些操作?将该变量作为会话参数 例如:
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.
}
这有助于你开始吗?;)
设置为:${yourVar}
另一种方法是在控制器端处理它,甚至在进入JSP之前。但是基本上是一样的-如果设置了变量,您可以使用session来保存它。像这样设置jsf页面的view标记的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}">
确保将bean添加到pageFlowScope后,代码就可以运行了…我使用adf创建页面。tnx:)我应该为请求导入什么?请求未知。我需要导入到我的库中的内容?导入javax.servlet.http.HttpSession;而不是HttpSession session=request.getSession(false);用法:HttpSession会话=((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession(false)@AmrGawish,仅当OP使用JSF时,没有迹象表明他使用JSF。您在第一次加载时从何处设置变量?啊哈。。那么我的回答对你没有帮助。。尝试读取构造函数中的会话变量,然后。。如果未设置,则设置,否则不执行任何操作。。这是同样的逻辑,只是在不同的地方。。
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
beforePhase="#{pageFlowScope.[YourClass]Bean.beforePhase}">
public void beforePhase(PhaseEvent phaseEvent)
{
FacesContext context = FacesContext.getCurrentInstance();
if(!context.getRenderKit().getResponseStateManager().isPostback(context))
{
//Do your thing.. (Set your variable etc.)
}
}