My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 31 mai 2015

[OmniFaces utilities 2.0] Get the ServletRegistration associated with the FacesServlet


[OmniFaces utilities] The getFacesServletRegistration() returns the ServletRegistration associated with the FacesServlet.

Method:
Usage:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
ServletRegistration servletRegistration = Platform.getFacesServletRegistration(request.getServletContext());
// e.g. javax.faces.webapp.FacesServlet
LOG.info("Servlet registration class name: " + servletRegistration.getClassName());

[OmniFaces utilities 2.0] Bean Validation - Check if Bean Validation is available


[OmniFaces utilities] The isBeanValidationAvailable() returns true if the Bean Validation is available.

Method:
Usage:

Whenever you need to check if the Bean Validation is available, you can use the OmniFaces utility method, Platform#isBeanValidationAvailable():

boolean isBeanValidation = Platform.isBeanValidationAvailable();
if(isBeanValidation){
   // do something
} else {
   // do something else
}

[OmniFaces utilities 2.0] Bean Validation - Get a Validator instance


[OmniFaces utilities] The getBeanValidator() returns a Validator instance.

Method:
Usage:

Those who are familiar with Bean Validation and implicitly with javax.validation.Validation, knows that Validation instances can be obtained via ValidatorFactory#getValidator(). OmniFaces provides the Platform#getBeanValidator() for this goal, and it can be used as below:

import org.omnifaces.util.Platform;
import javax.validation.ValidatorFactory;
...
Validator validator=Platform.getBeanValidator();

sâmbătă, 30 mai 2015

[OmniFaces utilities 2.0] Bean Validation - bootstrap the Validation using the default validation provider resolver


[OmniFaces utilities] The getBeanValidatorFactory() bootstraps the Validation using the default validation provider resolver.

Method:
Usage:

Those who are familiar with Bean Validation and implicitly with javax.validation.Validation, knows that there are three ways to bootstrap the Validation:

·         using the Validation#buildDefaultValidatorFactory()
·         choosing a custom ValidationProviderResolver
·         specifying explicitly and in a type safe fashion the expected provider

As you can see from the source code listed above, OmniFaces uses the first approach. So, whenever you need to bootstrap the javax.validation.Validation via the Validation#buildDefaultValidatorFactory(), you can use the OmniFaces shortcut, getBeanValidatorFactory() as below:

import org.omnifaces.util.Platform;
import javax.validation.ValidatorFactory;
...
ValidationFactory validationFactory = Platform.getBeanValidatorFactory();

[OmniFaces utilities 2.0] Working with OmniFaces faces message builder


[OmniFaces utilities] The Message class represents the OmniFaces faces message builder.

Class:
See also: Faces#getFlash()
Usage:

In order to use the OmniFaces faces message builder, we start from a  faces message with the default INFO severity and the given message body which is formatted with the given parameters as summary message. This message can be obtained via Messages#create() method as below:

import org.omnifaces.util.Messages;
import org.omnifaces.util.Messages.Message;
...
private static final String FILES_UPLOADED_ERROR = "The {0} file cannot be uploaded ! File size should be smaller than {1} bytes !";
...
Message msg = Messages.create(FILES_UPLOADED_ERROR, "order.txt", 1024);

Now, we can:
- set the detail message via Message#detail(String, Object...)
- change the default INFO severity via Message#warn(), Message#error(), or Message#fatal()
- make it a flash message via Message#flash()
- add it to the faces context via Message#add(String) to add it for a specific client ID
- or, add it to the faces context via Message#add() to add it as a global message

For example, we can change the default INFO severity in ERROR, make it a flash message and add it in the faces context as a global message, as below:

msg.error();
msg.flash();
msg.add();
       
Or, you can put everything in a compact line:

Messages.create(FILES_UPLOADED_ERROR, "order.txt", 1024).error().flash().add();

The resulted message will be:

// ERROR message
The order.txt file cannot be uploaded ! File size should be smaller than 1,024 bytes.

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, <h:message>, for component messages, or any other approach capable to display the messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

joi, 28 mai 2015

[OmniFaces utilities 2.0] Add globally a parameterized flash scoped FATAL faces message


[OmniFaces utilities] The addFlashGlobalFatal() method adds a flash scoped global FATAL faces message, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
User:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global FATAL parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// FATAL message
private static final String USER_FATAL = "The database at {0} does not respond on port {1} !";
...
public String myAction() {
// add an FATAL message as a global message in flash scope
Object[] params = new Object[]{"89.38.122.5", "5432"};
Messages.addFlashGlobalFatal(USER_FATAL, params);
// or
Messages.addFlashGlobalFatal(USER_FATAL, "89.38.122.5", "5432");

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// FATAL message
The database at 89.38.122.5 does not respond on port 5432 !

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, or any other approach capable to display the global messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

[OmniFaces utilities 2.0] Add globally a parameterized flash scoped ERROR faces message


[OmniFaces utilities] The addFlashGlobalError() method adds a flash scoped global ERROR faces message, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
Usage:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global ERROR parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// ERROR message
private static final String FILES_UPLOADED_ERROR = "The {0} file cannot be uploaded ! File size should be smaller than {1} bytes.";
...
public String myAction() {
// add an ERROR message as a global message in flash scope
Object[] params = new Object[]{"order.txt", 1024};
Messages.addFlashGlobalError(FILES_UPLOADED_ERROR, params);
// or
Messages.addFlashGlobalError(FILES_UPLOADED_ERROR, "order.txt", 1024);

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// ERROR message
The order.txt file cannot be uploaded ! File size should be smaller than 1,024 bytes.

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, or any other approach capable to display the global messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

[OmniFaces utilities 2.0] Add globally a parameterized flash scoped WARN faces message


[OmniFaces utilities] The addFlashGlobalWarn() method adds a flash scoped global WARN faces message, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
Usage:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global WARN parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// WARN message
private static final String USER_WARN = "The {0} user has reached the maximum number, {1}, of submissions !";
...
public String myAction() {
// add an WARN message as a global message in flash scope
Object[] params = new Object[]{"Mark Rightwer", 5};
Messages.addFlashGlobalWarn(USER_WARN, params);
// or
Messages.addFlashGlobalWarn(USER_WARN, "Mark Rightwer", 5);

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// WARN message
The Mark Rightwer user has reached the maximum number, 5, of submissions !

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, or any other approach capable to display the global messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

[OmniFaces utilities 2.0] Add globally a parameterized flash scoped INFO faces message


[OmniFaces utilities] The addFlashGlobalInfo() method adds a flash scoped global INFO faces message, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
Usage:
The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global INFO parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// INFO message
private static final String FILES_UPLOADED = "The {0} file and associated extensions ({1}, {2} and {3}) were successfully uploaded !";
...
public String myAction() {
// add an INFO message as a global message in flash scope
Object[] params = new Object[]{"SHP", "SHX", "DBF", "SBN"};
Messages.addFlashGlobalInfo(FILES_UPLOADED, params);
// or
Messages.addFlashGlobalInfo(FILES_UPLOADED, "SHP", "SHX", "DBF", "SBN");

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// INFO message
The SHP file and associated extensions (SHX, DBF and SBN) were successfully uploaded !

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, or any other approach capable to display the global messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

[OmniFaces utilities 2.0] Add globally a flash scoped faces message


[OmniFaces utilities] The addFlashGlobal() method adds a flash scoped global faces message. This adds a faces message to a clientId of null. Use this when you need to display the message after a redirect.

Method:
Usage:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global INFO message and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
public String myAction() {
 FacesMessage message = new FacesMessage();
 message.setSummary("This is page B!");
 message.setSeverity(FacesMessage.SEVERITY_INFO);       
 Messages.addFlashGlobal(message);
 // do not try the Messages#addGlobal() because the message will not "survive" during redirect
 //Messages.addGlobal(message);
 return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see the message:  This is page B!

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, or any other approach capable to display the global messages.

miercuri, 27 mai 2015

OmniFaces 2.1-RC2 has been released!




[OmniFaces utilities 2.0] Add in flash scope a parameterized global (or to the given client ID) FATAL faces message


[OmniFaces utilities] The addFlashFatal() method adds a flash scoped FATAL faces message to the given clientId, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
Usage:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global FATAL parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// FATAL message
private static final String USER_FATAL = "The database at {0} does not respond on port {1} !";
...
// for a certain component, replace null with clientId
public String myAction() {
// add a FATAL message as a global message in flash scope
Object[] params = new Object[]{"89.38.122.5", "5432"};
Messages.addFlashFatal(null, USER_FATAL, "89.38.122.5", "5432");
// or
Messages.addFlashFatal(null, USER_FATAL, params);

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// FATAL message
The database at 89.38.122.5 does not respond on port 5432 !

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, <h:message>, for component messages, or any other approach capable to display the messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

[OmniFaces utilities 2.0] Add in flash scope a parameterized global (or to the given client ID) WARNING faces message


[OmniFaces utilities] The addFlashWarn() method adds a flash scoped WARNING faces message to the given clientId, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
Usage:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global WARNING parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// WARN message
private static final String USER_WARN = "The {0} user has reached the maximum number, {1}, of submissions !";
...
// for a certain component, replace null with clientId
public String myAction() {
// add a WARNING message as a global message in flash scope
Object[] params = new Object[]{"Mark Rightwer", 5};
Messages.addFlashWarn(null, USER_WARN, "Mark Rightwer", 5);
// or
Messages.addFlashWarn(null, USER_WARN, params);

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// WARN message
The Mark Rightwer user has reached the maximum number, 5, of submissions !

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, <h:message>, for component messages, or any other approach capable to display the messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

[OmniFaces utilities 2.0] Add in flash scope a parameterized global (or to the given client ID) ERROR faces message


[OmniFaces utilities] The addFlashError() method adds a flash scoped ERROR faces message to the given clientId, with the given message body which is formatted with the given parameters. Use this when you need to display the message after a redirect.

Method:
Usage:

The information stored in flash scope "survives" one redirect. Let's suppose that the below code is in page A.xhtml:

<h:form>
 <h:commandButton value="Click Me!" action="#{myBean.myAction()}"/>
</h:form>

The myAction() method perform some tasks, including generating a global ERROR parameterized messages and redirecting to page, B.xhtml:

import org.omnifaces.util.Messages;
...
// ERROR message
private static final String FILES_UPLOADED_ERROR = "The {0} file cannot be uploaded ! File size should be smaller than {1} bytes.";
...
// for a certain component, replace null with clientId
public String myAction() {
// add an ERROR message as a global message in flash scope
Object[] params = new Object[]{"order.txt", 1024};
Messages.addFlashError(null, FILES_UPLOADED_ERROR, "order.txt", 1024);
// or
Messages.addFlashError(null, FILES_UPLOADED_ERROR, params);

return "B?faces-redirect=true";
}

Now, in page B.xhtml, you will see this message:

// ERROR message
The order.txt file cannot be uploaded ! File size should be smaller than 1,024 bytes.

Note Don't forget to use in page where the messages are displayed the <h:messages>, for global messages, <h:message>, for component messages, or any other approach capable to display the messages.

Note By default, this example uses the OmniFaces default message resolver, but you can use your own message resolver as in Working with OmniFaces Message Resolvers.

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors