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 Deployed on GAE production environment, when navigate from one page/view to another page/view, GAE shows error message “View xxx could not be restored“? JSF 2.1.7 Google App Engine SDK 1.6.3 P.S No problem at local GAE development. Solution By default, JSF 2 is using server for session management, and it’s not supported in GAE […]

Read more GAE + JSF : View /hello.xhtml could not be restored

Problem JSF application is able to deploy on local GAE environment, with following development environment : JSF 2.1.7 Google App Engine SDK 1.6.3 But hit error message while deployed on GAE production environment. Below is the logged error messages from GAE. com.sun.faces.config.ConfigureListener installExpressionFactory: Unable to instantiate ExpressionFactory ‘com.sun.el.ExpressionFactoryImpl’ E 2012-04-24 03:37:37.989 com.sun.faces.config.ConfigureListener contextInitialized: Critical error […]

Read more GAE + JSF : Unable to instantiate ExpressionFactory ‘com.sun.el.ExpressionFactoryImpl’

Problem Developing JSF (2.1.7) web application with Google App Engine (1.6.3), hits following error message : java.lang.NoClassDefFoundError: javax.naming.InitialContext is a restricted class. Please see the Google App Engine developer’s guide for more details. at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51) at com.sun.faces.config.WebConfiguration.processJndiEntries(WebConfiguration.java:687) at com.sun.faces.config.WebConfiguration.<init>(WebConfiguration.java:134) at com.sun.faces.config.WebConfiguration.getInstance(WebConfiguration.java:194) at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:163) //… Solution Java clsss – javax.naming.InitialContext, is not supported in Google App […]

Read more GAE + JSF : javax.naming.InitialContext is a restricted class

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

The standard JSF conversion and validation error messages are too detail, technical or sometime, not really human readable. In this article, it shows you how to customize standard conversion or validation error message in JSF 2.0. Summary Guide Find your message key from jsf-api-2.x.jar, “Messages.properties” file. Create your own properties file, and put the same […]

Read more Customize validation error message in JSF 2.0

Problem See following JSF example : <h:inputSecret id="password" value="#{user.password}" size="20" required="true" label="Password"> <f:validateRegex pattern="((?=.*\d).{6,20})" /> </h:inputSecret> <h:message for="password" style="color:red" /> <h:commandButton value="Cancel" action="cancel" /> <h:commandButton value="Submit" action="result" /> If you click on the “cancel” button, the password validation will be fired and stop you proceed on the cancel page, which is doesn’t make sense. Is […]

Read more How to skip validation in JSF

“f:validateRegex” is a new validator tag in JSF 2.0, which is used to validate JSF component with a given regular expression pattern. For example, <h:inputSecret id="password" value="#{user.password}"> <f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" /> </h:inputSecret> The above regex pattern is required 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter […]

Read more JSF 2 validateRegex example

“f:validateRequired” is a new validator tag in JSF 2.0, which is used to make sure the input field is not empty. For example, <h:inputSecret id="password" value="#{user.password}"> <f:validateRequired /> </h:inputSecret> Alternatively, you can use the “required” attribute also, both are doing the same empty value validation. <h:inputSecret id="password" value="#{user.password}" required="true" /> “f:validateRequired” example A JSF 2.0 […]

Read more JSF 2 validateRequired example

“f:validateDoubleRange” is a JSF range validator tag, which is used to validate the range of a floating point value. For example, <h:inputText id="salary" value="#{user.salary}"> <f:validateDoubleRange minimum="10.11" maximum="10000.11" /> </h:inputText> When this form is submitted, the validator will make sure the “salary” value is within the range from “10.11” to “10000.11”. “f:validateDoubleRange” example A JSF 2.0 […]

Read more JSF 2 validateDoubleRange example

“f:validateLongRange” is a JSF range validator tag, which is used to check the range of a numeric value. For example, <h:inputText id="age" value="#{user.age}"> <f:validateLongRange minimum="1" maximum="150" /> </h:inputText> When this form is submitted, the validator will make sure the “age” value is within the range from 1 to 150. “f:validateLongRange” example A JSF 2.0 example […]

Read more JSF 2 validateLongRange example

“f:validateLength” is a JSF string length validator tag, which is used to check the length of a string. For example, <h:inputText id="username" value="#{user.username}"> <f:validateLength minimum="5" maximum="10" /> </h:inputText> When this form is submitted, the validator will make sure the “username” text field contains a minimum length of 5, maximum length of 10. “f:validateLength” example A […]

Read more JSF 2 validateLength example

Problem In JSF web application , load a message bundle in application level like this : faces-config.xml <?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> <application> <message-bundle> com.mkyong.payment_error </message-bundle> </application> </faces-config> When the page is rendered, it hits “Can’t find bundle for base name com.mkyong.payment_error, locale en_US“? Solution Obviously, the bundle or properties file […]

Read more Can’t find bundle for base name xxx, locale en_US

Problem A Java web application deployed in Tomcat, hits the following error message : javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ExpressionFactory org.apache.jasper.servlet.JspServlet.service(JspServlet.java:268) //… java.lang.ClassNotFoundException: javax.el.ExpressionFactory java.net.URLClassLoader$1.run(URLClassLoader.java:200) java.security.AccessController.doPrivileged(Native Method) java.net.URLClassLoader.findClass(URLClassLoader.java:188) //… javax.servlet.http.HttpServlet.service(HttpServlet.java:717) note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs. Solution The “javax.el.ExpressionFactory” class is belong to the “el-api.jar” library, you can […]

Read more java.lang.ClassNotFoundException: javax.el.ExpressionFactory

f:convertDateTime” is a standard JSF converter tag, which converts String into a specified “Date” format. In addition, it’s used to implement the date validation as well. The following JSF 2.0 example shows you how to use this “f:convertDateTime” tag. 1. Managed Bean A simple managed bean, with a “date” property. package com.mkyong; import java.io.Serializable; import […]

Read more JSF 2 convertDateTime example