Technology: Primefaces it’s java based web framework to develop web applications using java. It’s one of the compliance frameworks for JSF, there are many other frameworks like omnifaces, richfaces. Primefaces has rich UI components. In this tutorial primefaces, maven, java8, glasifish servers are used. Use case: If you want to open a new page by […]

Read more PrimeFaces – Open Window By Dropdown Selection

Here’s the scenario, create a custom JSF validator, injects a bean via Spring’s @Autowired. UsernameValidator.java – Custom JSF validator package com.mkyong.user; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.FacesValidator; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.mkyong.user.bo.UserService; @Component @Scope("request") @FacesValidator("UsernameValidator") public class UsernameValidator implements Validator { @Autowired UserService userService; @Override public […]

Read more Spring @Autowired into JSF custom validator

See a simple JSF example : <h:form id="signup-form"> <h:inputText id="email" value="#{beanBean.email}" /> </h:form> It will generates following HTML code : <input id="signup-form:email" type="text" /> Uses jQuery selector to get the email id, but failed. <script> jQuery(document).ready(function($) { $(‘#signup-form:email’).checkEmailFormat(); }); </script> Solution This is a well-known problem to integrate JSF and jQuery – the colon “:” […]

Read more How to get JSF id via jQuery

To validate email, uses JSF <f:validateRegex>, and puts following regular expression. This regex should be able to validates most of the email format, and I’m using it for few projects. Email Regular Expression ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$ P.S For detail explanation, refer to this how to validate email address with regular expression. In this tutorial, we will show […]

Read more PrimeFaces + JSF Email validator example

In PrimeFaces, your can use <p:watermark> to display watermark effect on input field. This watermark component is using HTML5 placeholder attribute in supported browsers like Safari, Chrome and Firefox, and fall back to JavaScript solution for non-support browser like IE. Display watermark text in input field <p:inputText id="username" required="true" label="username" size="40" value="#{userBean.username}" /> <p:watermark for="username" […]

Read more PrimeFaces – Watermark on text input

Often times, you may need to override default PrimeFaces CSS with your pretty customize values. In this example, we will show you how to override PrimeFaces error message style. Debug with FireBug, the PF’s error messages style are from primefaces.css primefaces.css .ui-message-info, .ui-message-error, .ui-message-warn, .ui-message-fatal { border: 1px solid; margin: 0px 5px; padding: 2px 5px; […]

Read more How to override PrimeFaces CSS?

The PrimeFaces showcase is showing how to use the entire 100+ PrimeFaces components, which is impressive! Actually, you can download the entire PrimeFaces showcase web application (in war) and source code, and deploy on your local server for testing or further study. Here we show you how. 1. Visit Maven Repository The entire PrimeFaces showcase […]

Read more Download PrimeFaces ShowCase and Source Code

In this tutorial, we will show you how to use PrimeFaces extensions – Maven plugin, to compress and combine JavaScript and CSS files, a web resources optimization example, to make web site load faster. Tool tested : PrimeFaces 3.3 PrimeFaces-Extensions 0.5 Maven 3.0.3 Note This guide is example-driven, for detail explanation and all other plugin […]

Read more PrimeFaces compress and combine JavaScript and CSS

By default, PrimeFaces will return total of four files, 2 css files and 2 Javascript files. <link type="text/css" rel="stylesheet" href="/primefaces/faces/javax.faces.resource/theme.css?ln=primefaces-aristo" /> <link type="text/css" rel="stylesheet" href="/primefaces/faces/javax.faces.resource/primefaces.css?ln=primefaces" /> <script type="text/javascript" src="/primefaces/faces/javax.faces.resource/jquery/jquery.js?ln=primefaces"></script> <script type="text/javascript" src="/primefaces/faces/javax.faces.resource/primefaces.js?ln=primefaces"></script> Some people don’t like the default PrimeFaces CSS file and wonder how to remove it? Solution You are allow to remove only the […]

Read more Remove all default CSS styling from PrimeFaces

Recently, testing on PrimeFaces’ idleMonitor component. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> </h:head> <h:body> <h1>PrimeFaces and idleMonitor</h1> <p:growl id="messages" showDetail="true" sticky="true" /> <p:idleMonitor timeout="10000" update="messages"> <p:ajax event="idle" listener="#{idleBean.idleListener}" update="messages" /> </p:idleMonitor> </h:body> </html> Problem After 10 seconds, console prompts java.io.IOException: Not in GZIP format? That’s weird, what GZIP to do iwith idleMonitor […]

Read more PrimeFaces : java.io.IOException: Not in GZIP format

The ServletContext class is important in web application, often times, you need this class get the information of current deployed servlet container. Here’s a trick to show you how to get ServletContext in JSF2, via FacesContext. ServletContext servletContext = (ServletContext) FacesContext .getCurrentInstance().getExternalContext().getContext(); References ServletContext JavaDoc FacesContext JavaDoc

Read more How to get ServletContext in JSF 2

In this tutorial, we show you how to run a Quartz job during JSF web application via QuartzInitializerListener listener class in Quartz library. This solution is not only works with JSF 2, the concept is applicable on almost all standard Java web application. Tools Used : JSF 2.1.11 Quartz 2.1.5 Maven 3 Eclipse 4.2 Tomcat […]

Read more JSF 2 + Quartz 2 example

Problem JSF 2.0 web application, a managed bean uses @Resource to inject the “jdbc/mkyongdb” datasource into a ds property. @ManagedBean(name="customer") @SessionScoped public class CustomerBean implements Serializable{ //resource injection @Resource(name="jdbc/mkyongdb") private DataSource ds; When deployed to Tomcat 6, it hits following error messages for the MySQL datasource configuration. com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on […]

Read more javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

In JSF 2.0, you can attach a javax.faces.event.PreRenderViewEvent system event to perform custom task before a view root (JSF page) is display. Let see a complete PreRenderViewEvent example below : 1. Managed Bean Create a normal bean, contains a method signature “public void method-name(ComponentSystemEvent event)“, later you will ask listener to call this method. In […]

Read more JSF 2 PreRenderViewEvent example

Since JSF 2.0, you can register javax.faces.event.PostConstructApplicationEvent and javax.faces.event.PreDestroyApplicationEvent system event to manipulate the JSF application life cycle. 1. PostConstructApplicationEvent – Perform a custom post-configuration after application has started. 2. PreDestroyApplicationEvent – Perform a custom cleanup task before application is about to be shut down. Note In JSF, you can’t depends on the standard ServletContextListeners […]

Read more JSF 2 PostConstructApplicationEvent and PreDestroyApplicationEvent example

As i know,there are 4 ways to pass a parameter value from JSF page to backing bean : Method expression (JSF 2.0) f:param f:attribute f:setPropertyActionListener Let see example one by one : 1. Method expression Since JSF 2.0, you are allow to pass parameter value in the method expression like this #{bean.method(param)}. JSF page… <h:commandButton […]

Read more 4 ways to pass parameter from JSF page to backing bean

In JSF, “f:setPropertyActionListener” tag is allow you to set a value directly into the property of your backing bean. For example, <h:commandButton action="#{user.outcome}" value="Submit"> <f:setPropertyActionListener target="#{user.username}" value="mkyong" /> </h:commandButton> In above JSF code snippets, if the button is clicked, it will set the “mkyong” value to the “username” property via setUsername() method. @ManagedBean(name="user") @SessionScoped public […]

Read more JSF 2 setPropertyActionListener example

In JSF, “f:attribute” tag allow you to pass a attribute value to a component, or a parameter to a component via action listener. For example, 1. Assign a attribute value to a component. <h:commandButton"> <f:attribute name="value" value="Click Me" /> </h:commandButton> //is equal to <h:commandButton value="Click Me" /> 2. Assign parameter to a component and get […]

Read more JSF 2 attribute example

In JSF, “f:param” tag allow you to pass a parameter to a component, but it’s behavior is different depends on which type of component it’s attached. For example, 1. f:param + h:outputFormat If attach a “f:param” tag to “h:outputFormat“, the parameter is specifies the placeholder. <h:outputFormat value="Hello,{0}. You are from {1}."> <f:param value="JSF User" /> […]

Read more JSF 2 param example

In JSF application, you can change your application locale programmatically like this : //this example change locale to france FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(‘fr’); It makes JSF support for internationalization or multiple languages easily. Complete JSF internationalization example In this tutorial, we show you a JSF 2.0 web application, which display a welcome page, retrieve a welcome message […]

Read more JSF 2 internationalization example

Problem How can a JSF event listener class access another managed bean? See scenario below : JSF page… <h:selectOneMenu value="#{country.localeCode}" onchange="submit()"> <f:valueChangeListener type="com.mkyong.CountryValueListener" /> <f:selectItems value="#{country.countryInMap}" /> </h:selectOneMenu> country managed bean… package com.mkyong; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="country") @SessionScoped public class CountryBean implements Serializable{ private String localeCode; public void setLocaleCode(String localeCode) { this.localeCode […]

Read more Access a managed bean from event listener – JSF

When user make changes in input components, such as h:inputText or h:selectOneMenu, the JSF “value change event” will be fired. Two ways to implement it : 1. Method binding – In input component, specified a bean’s method directly in the “valueChangeListener” attribute. JSF… <h:selectOneMenu value="#{bean.value}" onchange="submit()" valueChangeListener="#{bean.valueChangeMethod}"> <f:selectItems value="#{bean.values}" /> </h:selectOneMenu> Java… The method which […]

Read more JSF 2 valueChangeListener example

In this article, we will show you how to create a custom validator in JSF 2.0 Steps Create a validator class by implements javax.faces.validator.Validator interface. Override validate() method. Assign an unique validator ID via @FacesValidator annotation. Reference custom validator class to JSF component via f:validator tag. A detail guide to create a custom validator name […]

Read more Custom validator in JSF 2.0

In this article, we show you how to create a custom converter in JSF 2.0. Steps 1. Create a converter class by implementing javax.faces.convert.Converter interface. 2. Override both getAsObject() and getAsString() methods. 3. Assign an unique converter ID with @FacesConverter annotation. 4. Link your custom converter class to JSF component via f:converter tag. Custom converter […]

Read more Custom converter in JSF 2.0