Oracle ADF - Storing temporary values in PageFlowScope created
at Run time
Greetings,
Some time you need to store temporary values on the page like a global variable. Oracle ADF provides various bean scopes like pageflow scope, session scope etc etc we can definitely use them
But can you create pageflow scope at run-time? the answer is YES
Lets explore a simple example
Example:
When you press the button on the screen it will count and display how many times you have pressed the button and counter will be different for each browser window or tab
Some time you need to store temporary values on the page like a global variable. Oracle ADF provides various bean scopes like pageflow scope, session scope etc etc we can definitely use them
But can you create pageflow scope at run-time? the answer is YES
Lets explore a simple example
Example:
When you press the button on the screen it will count and display how many times you have pressed the button and counter will be different for each browser window or tab
Technical detail:
·
We have an input text
component and its value using a page flow scope bean attribute called 'counter'
·
We got a button on
screen which is calling an action listener method doCounting()
How Example works:
·
When we pressed the
button on screen it creates a pageflow scope bean on runtime called 'myCounter'
and increment this value on every button pressed.
·
The value of myCounter
then assigned to pageFlowscope attribute in a managed bean called
'counter' which is visible on screen as output text
Following is the code on button with other helping methods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
//Method on the
button
public void doCounting(ActionEvent
actionEvent) {
Number
value = (Number)getPageFlowScopeValue("myCounter");
if (value.intValue()
>= 0) {
setPageFlowScopeValue("myCounter",
value.intValue() + 1);
}
setManagedBeanValue("pageFlowScope.pFlowBean.counter",
getPageFlowScopeValue("myCounter"));
}
//Method to
set the value of page flow scope created on runtime
public void setPageFlowScopeValue(String
name, Number value) {
ADFContext
adfCtx = ADFContext.getCurrent();
Map
pageFlowScope = adfCtx.getPageFlowScope();
pageFlowScope.put(name,
value);
}
//method to get the
value of page flow scope created on runtime
public Object
getPageFlowScopeValue(String name) {
ADFContext
adfCtx = ADFContext.getCurrent();
Map
pageFlowScope = adfCtx.getPageFlowScope();
Object
val = pageFlowScope.get(name);
if (val
== null)
return 0;
else
return val;
}
//Methods used to
get and set the values in a Managed bean
public Object
getManagedBeanValue(String beanName) {
StringBuffer
buff = new StringBuffer("#{");
buff.append(beanName);
buff.append("}");
return resolveExpression(buff.toString());
}
public Object
resolveExpression(String expression) {
FacesContext
facesContext = FacesContext.getCurrentInstance();
Application
app = facesContext.getApplication();
ExpressionFactory
elFactory = app.getExpressionFactory();
ELContext
elContext = facesContext.getELContext();
ValueExpression
valueExp =
elFactory.createValueExpression(elContext,
expression, Object.class);
return valueExp.getValue(elContext);
}
public void setManagedBeanValue(String
beanName, Object newValue) {
StringBuffer
buff = new StringBuffer("#{");
buff.append(beanName);
buff.append("}");
setExpressionValue(buff.toString(),
newValue);
}
public static void setExpressionValue(String
expression, Object newValue) {
FacesContext
facesContext = FacesContext.getCurrentInstance();
Application
app = facesContext.getApplication();
ExpressionFactory
elFactory = app.getExpressionFactory();
ELContext
elContext = facesContext.getELContext();
ValueExpression
valueExp =
elFactory.createValueExpression(elContext,
expression, Object.class);
Class
bindClass = valueExp.getType(elContext);
if (bindClass.isPrimitive()
|| bindClass.isInstance(newValue)) {
valueExp.setValue(elContext,
newValue);
}
}
|
Download the sample code
Note: There are many ways to do the same task one technique is demonstrated by Andrejus click here
===============================
hi i have httpsession how
i can get current user from session ?
public void afterPhase(PhaseEvent phaseEvent) {
FacesContext fc = phaseEvent.getFacesContext();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
UserData u = (UserData)session.getAttribute("user");
boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true :false;
if (!loginPage && u == null){
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "logout");
}
}
public void afterPhase(PhaseEvent phaseEvent) {
FacesContext fc = phaseEvent.getFacesContext();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
UserData u = (UserData)session.getAttribute("user");
boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true :false;
if (!loginPage && u == null){
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "logout");
}
}
===========================
down voteaccept |
Yes its possible
If the bean doesnot exits then put it in session first
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(key,object);
And to use the bean on xhtml page use
<h:outputText value="#{sessionScope.key}" />
=========================================
|
If the bean doesnot exits then put it in session first
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(key,object);
And to use the bean on xhtml page use
<h:outputText value="#{sessionScope.key}" />
====================================================
Is it possible to display the current date (today's) in JSF without using a backing bean?
I have the following code snippet , but it didn't work out.
<div class="leftSide">Today's date #{currentDate}</div>
or
<f:facet name="header">
<h:outputText value="Today's date" />
</f:facet>
<h:outputText value="#currentDate">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
==================================
You could register an instance of java.util.Date as a request scoped bean in faces-config.xml.
<managed-bean>
<managed-bean-name>currentDate</managed-bean-name>
<managed-bean-class>java.util.Date</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
This way it's available as #{currentDate} without the need for a custom backing bean class.
Update: the JSF utility library OmniFaces has such a bean already registered as #{now}. So if you happen to use OmniFaces already, you can just make use of it directly.
<h:outputText value="#{now}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
===================================
</h:outputText>
No comments:
Post a Comment