Using a “Please select” f:selectItem with null/empty value inside a p:selectOneMenu

When the select item value is null, then JSF won’t render <option value>, but only <option>. As consequence, browsers will submit the option’s label instead. This is clearly specified in HTML specification (emphasis mine): value = cdata [CS] This attribute specifies the initial value of the control. If this attribute is not set, the initial … Read more

How to show user-friendly error page in browser when runtime exception is thrown by servlet?

Just declare an <error-page> in web.xml wherein you can specify the page which should be displayed on a certain Throwable (or any of its subclasses) or a HTTP status code. E.g. <error-page> <exception-type>java.lang.Exception</exception-type> <location>/error.jsp</location> </error-page> which will display the error page on any subclass of the java.lang.Exception, but thus not java.lang.Throwable or java.lang.Error. This way … Read more

How to add placeholder attribute to JSF input component?

I thought everything that was not JSF was passed to the browswer for rendering? This assumption is thus wrong. Unspecified component attributes are ignored by the JSF renderers. You have basically the following options to get it to work: If you’re already on JSF 2.2 or newer, set it as a passthrough attribute. <… xmlns:a=”http://xmlns.jcp.org/jsf/passthrough”> … Read more

How to handle session expiration and ViewExpiredException in JSF 2?

To handle the exception whenever the user invokes a synchronous POST request on a page while the HTTP session has been expired and the JSF view state saving method is set to server, add an <error-page> to the web.xml which catches the JSF ViewExpiredException and shows the home page. <error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type> <location>/home.xhtml</location> </error-page> To handle … Read more

Format Date output in JSF

Use <f:convertDateTime>. You can nest this in any input and output component. Pattern rules are same as java.text.SimpleDateFormat. <h:outputText value=”#{someBean.dateField}” > <f:convertDateTime pattern=”dd.MM.yyyy HH:mm” /> </h:outputText>

How does EL empty operator work in JSF?

From EL 2.2 specification (get the one below “Click here to download the spec for evaluation”): 1.10 Empty Operator – empty A The empty operator is a prefix operator that can be used to determine if a value is null or empty. To evaluate empty A If A is null, return true Otherwise, if A … Read more

JSF backing bean structure (best practices)

You might want to check this out: making distinctions between different kinds of JSF managed beans. Here is a description of the different bean types, as defined in the above article by Neil Griffin: Model Managed-Bean: Normally session scope. This type of managed-bean participates in the “Model” concern of the MVC design pattern. When you … Read more