|
|
Search on blog
Petition by Java EE Guardians
joi, 30 aprilie 2015
miercuri, 29 aprilie 2015
[OmniFaces utilities (2.0)] Check if the current request is a PrimeFaces dynamic resource request
[OmniFaces utilities] The
isPrimeFacesDynamicResourceRequest()
method returns true
if the current request is a PrimeFaces dynamic resource request.Method:
Usage:
boolean is =
Hacks.isPrimeFacesDynamicResourceRequest(FacesContext.getCurrentInstance());
if(is){
// is
} else {
//is not
}
For example,
PrimeFaces generates dynamic resource requests when we try to load images from StreamedContent
via <p:graphicImage>.
Such request may look like in figure below:
If you write
a custom resource handler, it will be useful to distinguish such cases.
[OmniFaces utilities (2.0)] Get the default resource maximum age in milliseconds
[OmniFaces utilities] The
getDefaultResourceMaxAge()
method returns the default resource maximum age in milliseconds.Method:
See also: Faces#getInitParameter()
Usage:import org.omnifaces.util.Hacks;
...
// e.g. 604800000
milliseconds
long maxAge
= Hacks.getDefaultResourceMaxAge();
OmniFaces uses this method to set the Expires response header in org.omnifaces.resourcehandler.DynamicResource:
OmniFaces uses this method to set the Expires response header in org.omnifaces.resourcehandler.DynamicResource:
@Override
public
Map<String, String> getResponseHeaders() {
Map<String, String> responseHeaders =
new HashMap<>(RESPONSE_HEADERS_SIZE);
responseHeaders.put("Last-Modified",
formatRFC1123(new Date(getLastModified())));
responseHeaders.put("Expires",
formatRFC1123(new Date(System.currentTimeMillis() +
Hacks.getDefaultResourceMaxAge())));
responseHeaders.put("Etag",
String.format("W/\"%d-%d\"", getResourceName().hashCode(),
getLastModified()));
responseHeaders.put("Pragma",
""); // Explicitly set empty pragma to prevent some containers from
setting it.
return responseHeaders;
}
[OmniFaces utilities (2.0)] Remove the resource dependency processing related attributes
[OmniFaces utilities] The
removeResourceDependencyState()
method remove the resource dependency processing related attributes from the given faces context.Method:
import
org.omnifaces.util.Hacks;
...
Hacks.removeResourceDependencyState(FacesContext.getCurrentInstance());
In Mojarra,
the com.sun.faces.PROCESSED_RESOURCE_DEPENDENCIES
is declared in RequestStateManager#PROCESSED_RESOURCE_DEPENDENCIES,
and it is used to determine of the specified @ResourceDependency has
already been previously processed:
// Mojarra
2.2.9 - com.sun.faces.application.annotation.ResourceDependencyHandler
private
ResourceDependency[] dependencies;
...
@SuppressWarnings({"unchecked"})
private
boolean hasBeenProcessed(FacesContext ctx, ResourceDependency dep) {
Set<ResourceDependency> dependencies =
(Set<ResourceDependency>)
RequestStateManager.get(ctx,
RequestStateManager.PROCESSED_RESOURCE_DEPENDENCIES);
return ((dependencies != null) &&
dependencies.contains(dep));
}
And, to
indicate that the specified @ResourceDependency has been processed:
// Mojarra
2.2.9 - com.sun.faces.application.annotation.ResourceDependencyHandler
private
ResourceDependency[] dependencies;
...
@SuppressWarnings({"unchecked"})
private void
markProcssed(FacesContext ctx, ResourceDependency dep) {
Set<ResourceDependency> dependencies =
(Set<ResourceDependency>)
RequestStateManager.get(ctx,
RequestStateManager.PROCESSED_RESOURCE_DEPENDENCIES);
if (dependencies == null) {
dependencies = new
HashSet<ResourceDependency>(6);
RequestStateManager.set(ctx,
RequestStateManager.PROCESSED_RESOURCE_DEPENDENCIES, dependencies);
}
dependencies.add(dep);
}
marți, 28 aprilie 2015
OmniFaces is used in ZEEF.com architecture
Arjan Tijms has been interviewed by Adam Bien about the architecture of ZEEF.com. Among others, Arjan Tijms "reveals" the fact that OmniFaces is one of the external dependencies used in ZEEF.com.
Read further here ...
Read further here ...
[OmniFaces utilities (2.0)] Stream the given input to the given output by NIO ByteBuffer
[OmniFaces utilities] The
stream()
method stream the given input to the given output by NIO ByteBuffer
. Both the input and output streams will implicitly be closed after streaming, regardless of whether an exception is been thrown or not.Method:
Usage:
Suppose we have a file as an input stream and we want to pass it to an output stream for further processing. For this, we can use the Utils#stream() method:
import java.io.InputStream;
import java.io.OutputStream;
import org.omnifaces.util.Utils;
...
InputStream inputStream;
OutputStream outputStream;
try {
inputStream = new
FileInputStream("input_file");
outputStream = new
FileOutputStream("output_file");
long length = Utils.stream(inputStream,
outputStream);
} catch (IOException ex) {
//
}
}
By default, the Utils#stream() method returns the length
of the written bytes.
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
[OmniFaces utilities (2.0)] Check if the given class has at least one of the given annotations
[OmniFaces utilities] The
isOneAnnotationPresent()
method returns true
if the given class has at least one of the given annotations.Method:
Usage:
Check if the specified class is a JSF/CDI managed bean:
import org.omnifaces.util.Utils;
import javax.inject.Named;
import javax.faces.bean.ManagedBean;
...
Class[] annotations = new Class[]{Named.class,
ManagedBean.class};
boolean
isAnyAnnotations = Utils.isOneAnnotationPresent(MyClass.class, annotations);
Check if the
specified class is a scoped/session/application CDI managed bean:
import org.omnifaces.util.Utils;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
...
Class[] annotations = new Class[]{RequestScoped.class,
SessionScoped.class, ApplicationScoped.class};
boolean
isAnyAnnotations = Utils.isOneAnnotationPresent(MyCDIBean.class, annotations);
luni, 27 aprilie 2015
[OmniFaces utilities (2.0)] Check if the given class instance could also be an instance of one of the given classes
[OmniFaces utilities] The
isOneInstanceOf()
method returns true
if an instance of a class could also be an instance of one of the given classes.Method:
Usage:
UIComponent currentComponent = Components.getCurrentComponent();
// check if the current component is an UICommand
boolean isCommand =
Utils.isOneInstanceOf(currentComponent.getClass(), HtmlCommandButton.class,
HtmlCommandLink.class);
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
[OmniFaces utilities (2.0)] Check if the given string starts with one of the given prefixes
[OmniFaces utilities] The
startsWithOneOf()
method returns true
if the given string starts with one of the given prefixes.Method:
Usage:
import org.omnifaces.util.Utils;
...
String[] listenersPrefixNames = {"Action", "AjaxBehavior",
"Behavior", "ComponentSystemEvent", "ViewMap",
"SystemEvent", "ValueChange"};
// returns true
boolean startsWithOneOf = Utils.startsWithOneOf("ComponentSystemEvent",
listenersPrefixNames);
// returns false
boolean startsWithOneOf = Utils.startsWithOneOf("Phase",
listenersPrefixNames);
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
[OmniFaces utilities (2.0)] Check if the given object equals one of the given objects
[OmniFaces utilities] The
isOneOf()
method returns true
if the given object equals one of the given objects.Method:
Usage:
The current
submitted form has ID, loginFormId. Now, let's see
some tests for Utils#isOneOf():
UIViewRoot viewRoot = Faces.getViewRoot();
UIComponent someForm=
viewRoot.findComponent("loginFormId");
UIForm currentForm = Components.getCurrentForm();
UICommand currentCommand = Components.getCurrentCommand();
UIForm closestForm = Components.getClosestParent(currentCommand,
UIForm.class);
UIComponent currentComponent = Components.getCurrentComponent();
boolean isSomeFormCurrentForm
= Utils.isOneOf(someForm, currentForm); // true
boolean
isSomeFormClosestForm = Utils.isOneOf(someForm, closestForm); // true
boolean
isSomeFormCurrentFormOrClosestForm = Utils.isOneOf(someForm,
currentForm, closestForm); // true
boolean
isSomeFormCurrentCommandOrCurrentComponent = Utils.isOneOf(someForm,
currentCommand, currentComponent); // false
See also:
Faces#getViewRoot() | Components#getCurrentForm() | Components#getCurrentCommand() |
Components#getClosestParent() | Components#getCurrentComponent()
Faces#getViewRoot() | Components#getCurrentForm() | Components#getCurrentCommand() |
Components#getClosestParent() | Components#getCurrentComponent()
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
[OmniFaces utilities (2.0)] Get the first non-null object of the argument list, or null if there is no such element
[OmniFaces utilities] The
coalesce()
method returns the first non-null
object of the argument list, or null
if there is no such element.Method:
Usage:
import org.omnifaces.util.Utils;
...
Object object = null;
StringBuilder sb = null;
BigDecimal ten = BigDecimal.TEN;
// returns BigDecimal.TEN, 10
Object notNull = Utils.coalesce(object, sb, ten);
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
[OmniFaces utilities (2.0)] Check if the given string is parseable as a number/decimal
[OmniFaces utilities] The
isNumber()
method returns true
if the given string is parseable as a number. I.e. it is not null, nor blank and contains solely digits. I.e., it won't throw a NumberFormatException
when parsing as Long
.[OmniFaces utilities] The
isDecimal()
method returns true
if the given string is parseable as a decimal. I.e. it is not null, nor blank and contains solely digits. I.e., it won't throw a NumberFormatException
when parsing as Double
.Method Utils#isNumber():
Method Utils#isDecimal():
Usage:
import org.omnifaces.util.Utils;
...
String a = "";
String b = "3";
String c = "3L";
String d = "3.14";
String e = "3.14f";
String f = "3.14d";
// a
boolean isANumber = Utils.isNumber(a); // false
boolean isADecimal = Utils.isDecimal(a); // false
// b
boolean isBNumber = Utils.isNumber(b); // true
boolean isBDecimal = Utils.isDecimal(b); // true
// c
boolean isCNumber = Utils.isNumber(c); // false
boolean isCDecimal = Utils.isDecimal(c); // false
// d
boolean isDNumber = Utils.isNumber(d); // false
boolean isDDecimal = Utils.isDecimal(d); // true
// e
boolean isENumber = Utils.isNumber(e); // false
boolean isEDecimal = Utils.isDecimal(e); // true
// f
boolean isFNumber = Utils.isNumber(f); // false
boolean isFDecimal = Utils.isDecimal(f); // true
Etichete:
inputSecret
,
OmniFaces 2.0
,
OmniFaces utilities
[OmniFaces utilities (2.0)] Check if the given string is null or is empty or contains whitespace only
[OmniFaces utilities] The
isBlank()
method returns true
if the given string is null
or is empty or contains whitespace only. In addition to #isEmpty(String)
, this thus also returns true
when string.trim().isEmpty()
returns true
.Method:
See also: Utils#isEmpty()
Usage:
import org.omnifaces.util.Utils;
...
String a = null;
String b="";
String c=" ";
String d="text";
boolean isABlank = Utils.isBlank(a); // true
boolean isBBlank = Utils.isBlank(b); // true
boolean isCBlank = Utils.isBlank(c); // true
boolean isDBlank = Utils.isBlank(d); // false
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
duminică, 26 aprilie 2015
[OmniFaces utilities (2.0)] Check if a String, Collection, Map or Array is empty
[OmniFaces utilities] The
isEmpty()
method returns true
if the given value is null
or is empty. Types of String
, Collection
, Map
and Array
are recognized. If none is recognized, then examine the emptiness of the toString()
representation instead.Methods:
This method uses the below ones (which can be invoked individually):
Testing a String:
String aString = null;
String bString = "";
String cString = "Done!";
import org.omnifaces.util.Utils;
...
boolean a = Utils.isEmpty(aString); // true
boolean b = Utils.isEmpty(bString); // true
boolean c = Utils.isEmpty(cString); // false
Testing an Object[]:
Object[] aObjectArray = null;
Object[] bObjectArray = new Object[1];
Object[] cObjectArray = {1};
boolean a = Utils.isEmpty(aObjectArray); // true
boolean b = Utils.isEmpty(bObjectArray); // false
boolean c = Utils.isEmpty(cObjectArray); // false
Testing a List:
List aList = null;
List bList = new ArrayList();
List<String> cList = new ArrayList<>();
ArrayList<String> dList = new ArrayList<String>() {
{
add("A");
add("B");
add("C");
}
};
boolean a =
Utils.isEmpty(aList); // true
boolean b =
Utils.isEmpty(bList); // true
boolean c =
Utils.isEmpty(cList); // true
boolean d =
Utils.isEmpty(dList); // false
Testing a Map:
Testing a Map:
Map aMap = null;
Map bMap = new HashMap();
Map<String, String> cMap = new HashMap<>();
Map<Byte, Integer> dMap = new HashMap<Byte,
Integer>() {
{
put(new
Byte("1"), 1);
put(new
Byte("2"), 2);
};
};
boolean a =
Utils.isEmpty(aMap); // true
boolean b =
Utils.isEmpty(bMap); // true
boolean c =
Utils.isEmpty(cMap); // true
boolean d = Utils.isEmpty(dMap); // false
Etichete:
JSF 2.2
,
OmniFaces 2.0
,
OmniFaces utilities
vineri, 24 aprilie 2015
[OmniFaces utilities (2.0)] Check whether the given script resource is rendered
[OmniFaces utilities] The
isScriptResourceRendered()
method returns whether the given script resource is rendered.Method:
See also: Set the given script/stylesheet resource asrendered
FacesContext
context = ...;
boolean isScriptRendered
= Hacks.isScriptResourceRendered(context, new ResourceIdentifier(script_component))
if(isScriptRendered){
// is
} else {
//is
not
}
Note The OmniFaces org.omnifaces.resourcehandler.ResourceIdentifier is a convenience class to represent a resource identifier (maps resource name and library).
[OmniFaces utilities (2.0)] Set the given script/stylesheet resource as rendered
[OmniFaces utilities] The
setScriptResourceRendered()
method set the given script resource as rendered.[OmniFaces utilities] The
setStylesheetResourceRendered()
method set the given stylesheet resource as rendered.Method (for script):
Method (for stylesheet):
When we need to add any component (resource instance) as a resource in the view, we can use UIViewRoot#addComponentResource(). By any component (resource instance) we understand a resource instance which is rendered by a resource Renderer, as described in the Standard HTML RenderKit. For example, if you move a <script> component at the end of the <body>, you will probably do this:
UIViewRoot
view = context.getViewRoot();
view.addComponentResource(context,
script_component, "body");
Further, you
may need to mark this resource as rendered. You have to instruct JSF that the
script resource is already rendered, otherwise JSF will force the
auto-inclusion/rendering of the script resource. As BalusC points out, "in case of Mojarra and MyFaces, a context
attribute with key of name+library and a value of true has to be set in order
to disable auto-inclusion of the resource". Well, in Mojarra you can
easy see these words in code lines in com.sun.faces.renderkit.html_basic.ScriptRenderer
class, in encodeEnd()
method (similar we have for stylesheets resources in StylesheetRenderer). The relevant part is listed below:
@Override
public void
encodeEnd(FacesContext context, UIComponent component)
throws
IOException {
...
Map<Object, Object> contextMap =
context.getAttributes();
...
String name = (String)
attributes.get("name");
String library = (String)
attributes.get("library");
String
key = name + library;
...
...
// Ensure this script/stylesheet is not rendered more
than once per request
if
(contextMap.containsKey(key)) {
return;
}
contextMap.put(key,
Boolean.TRUE);
...
}
}
Since
there's no standard JSF API for doing this, OmniFaces accomplished this task (for scripts) via Hacks#setScriptResourceRendered():
FacesContext
context = ...;
Hacks.setScriptResourceRendered(context,
new ResourceIdentifier(script_component));
And, for stylesheets via Hacks#setStylesheetResourceRendered():
FacesContext
context = ...;
Hacks.setStylesheetResourceRendered(context, new ResourceIdentifier(stylesheet_component));
Note The OmniFaces org.omnifaces.resourcehandler.ResourceIdentifier is a convenience class to represent a resource identifier (maps resource name and library).
Abonați-vă la:
Postări
(
Atom
)
|
[JSF Page Author Beginner's Guide] |
Postări populare
-
If you like this article, I think you are going to like JSF 2.3 Tutorial as well.
-
Starting with JSF 2.3 the JSF "native" managed bean annotations are officially deprecated. So, there is no doubt now that CDI is ...
-
[OmniFaces utilities] The getRequestURL() method returns the HTTP request URL with query string, regardless of any forward. This is the f...
-
Let's checkout a common practice for declaring constants in Java using the public static final declaration: public class Circle { ...
-
[OmniFaces utilities] The getActionExpressionsAndListeners() method returns a list of all action expressions and listeners associated wit...
-
[OmniFaces utilities] The getCurrentActionSource() method returns the source of the currently invoked action, or null if there is none, ...
-
Before you read this post please read: Use a CDI alternative as a mock implementation for a stateless session bean So, since you are fa...
-
Starting with JSF 2.3, more exactly with m07 , we can take advantage of using the auto detection of convertors based on 1st UISelectMany it...
-
[OmniFaces utilities] The isSerializable() method returns true if the given object is serializable. Method: Usage: Example 1 - te...
-
[OmniFaces utilities] The reverseArray() function returns a copy of the array with items in reversed order. Function: Usage: Let...
OmniFaces/JSF Fans
-
▼
2015
(
516
)
- ► septembrie ( 38 )
-
▼
aprilie
(
74
)
- OmniFaces 2.1-RC1 has been released!
- [OmniFaces utilities (2.0)] Check if the current r...
- [OmniFaces utilities (2.0)] Get the default resour...
- [OmniFaces utilities (2.0)] Remove the resource de...
- OmniFaces is used in ZEEF.com architecture
- [OmniFaces utilities (2.0)] Stream the given input...
- [OmniFaces utilities (2.0)] Check if the given cla...
- [OmniFaces utilities (2.0)] Check if the given cla...
- [OmniFaces utilities (2.0)] Check if the given str...
- [OmniFaces utilities (2.0)] Check if the given obj...
- [OmniFaces utilities (2.0)] Get the first non-null...
- [OmniFaces utilities (2.0)] Check if the given str...
- [OmniFaces utilities (2.0)] Check if the given str...
- [OmniFaces utilities (2.0)] Check if a String, Col...
- [OmniFaces utilities (2.0)] Check whether the give...
- [OmniFaces utilities (2.0)] Set the given script/s...
- [OmniFaces utilitites (2.0)] Check if MyFaces is used
- Expose constants and public static methods in EL v...
- [OmniFaces utilities (2.0)] Encode the given bean ...
- [OmniFaces utilities (2.0)] Encode the given Map a...
- [OmniFaces utilities (2.0)] Encode the given array...
- [OmniFaces utilities (2.0)] Encode the given Colle...
- [OmniFaces utilities (2.0)] Encode the given Boole...
- [OmniFaces utilities (2.2)] Get the named env entr...
- [OmniFaces utilities (2.0)] Check if JUEL supports...
- JSF "loading" JavaScript - brief overview
- [OmniFaces utilities (2.0)] Get application scope ...
- [OmniFaces utilities (2.0)] Get session scope map ...
- [OmniFaces utilities (2.0)] Get view scope map | G...
- [OmniFaces utilities (2.0)] Check if JUEL (impleme...
- [OmniFaces utilities (2.0)] Get an ordered set of ...
- [OmniFaces utilities (2.0)] Check if a renderer ty...
- [OmniFaces utilities (2.0)] Get the wrapped Partia...
- [OmniFaces utilities (2.0)] Get the render IDs fro...
- [OmniFaces utilities (2.0)] Get the RichFaces Part...
- [OmniFaces utilities (2.0)] Check if RichFaces 4.0...
- [OmniFaces utilities (2.2/2.0)] Send the given fil...
- [OmniFaces utilities (2.0)] Get flash scope map | ...
- [OmniFaces utilities (2.0)] Get the absolute disk ...
- [OmniFaces utilities (2.0)] Get request scope map ...
- [OmniFaces utilities (2.0)] Get a set of available...
- [OmniFaces utilities (2.0)] Get the input stream f...
- [OmniFaces utilities (2.0)] Get the URL for an app...
- SlideShare - Introduction to OmniFaces
- [OmniFaces utilities (2.0)] Check the mime type fo...
- [OmniFaces utilities (2.0)] Get the application in...
- [OmniFaces utilities (2.0)] Get the servlet context
- [OmniFaces utilities (2.0)] Checks whether the HT...
- [OmniFaces utilities (2.0)] Get/set the HTTP sessi...
- [OmniFaces utilities (2.0)] Get the time of the pr...
- [OmniFaces utilities (2.0)] Get the time when the...
- [OmniFaces utilities (2.0)] Check whether the HTTP...
- [OmniFaces utilities (2.0)] Check whether the HTTP...
- [OmniFaces utilities (2.0)] Invalidates the curren...
- [OmniFaces utilities (2.0)] Get the unique identif...
- [OmniFaces utilities (2.0)] Returns the HTTP sessi...
- [OmniFaces utilities (2.1/2.0)] Remove the cookie ...
- [OmniFaces utilities (2.0)] Add a cookie with give...
- [OmniFaces utilities (2.0)] Collect by name the va...
- [OmniFaces utilities (2.0)] Get/check the currentl...
- [OmniFaces utilities 2.0] Programmatically login/a...
- [OmniFaces utilities (2.0)] Force/check response c...
- 5 Quick Tips for JSF Page Authors
- [OmniFaces utilities (2.0)] Check if we're current...
- [OmniFaces Utilities (2.0)] "Force" control to go ...
- [OmniFaces utilities (2.0)] Reset the current resp...
- Copy Objects via OmniFaces Copier API
- [OmniFaces utilities (2.0)] Check if the response ...
- OmniFaces 2.1 SNAPSHOT Showcase is here!
- [OmniFaces utilities (2.0)] Add a header with give...
- OmniFaces <o:validateBean> to control bean validat...
- [OmniFaces Utilities (2.0)] Send a HTTP response e...
- [OmniFaces utilities (2.1/2.0)] Send a temporary (...
- JSF FacesEvent demystified / OmniFaces FacesEvent ...
JSF/OmniFaces Resources |