Why Facelets is preferred over JSP as the view definition language from JSF 2.0 onwards?

True, JSP has some templating capabilities, but the biggest disadvantage of using JSP in JSF is that JSP writes to the response as soon as it encounters template text content, while JSF would like to do some pre/post processing with it. In JSF 1.0/1.1 the following JSF code <h:outputText value=”first”/> second <h:outputText value=”third”/> fourth would … Read more

Reset input fields without executing validation

The <p:commandButton> processes indeed by default the entire form (process=”@form”), you can change this by specifying only the current component in the process attribute. <p:commandButton value=”Reset” … process=”@this” /> However, this will fail if the form is already been validated beforehand. The input fields which have been marked invalid won’t be updated with the new … Read more

Get Request and Session Parameters and Attributes from JSF pages

You can get a request parameter id using the expression: <h:outputText value=”#{param[‘id’]}” /> param—An immutable Map of the request parameters for this request, keyed by parameter name. Only the first value for each parameter name is included. sessionScope—A Map of the session attributes for this request, keyed by attribute name. Section 5.3.1.2 of the JSF … Read more

Java EE6> Packaging JSF facelets (xhtml) and ManagedBeans as JAR

Yes, that’s definitely possible, assuming that you’re using JSF 2.0, part of Java EE 6. As to the managed beans and other JSF classes like validators, converters, etc, just annotate them with @ManagedBean, @FacesValidator, @FacesConverter, etc and package them in the JAR the usual way. You only need to provide a JSF 2.0 compatible /META-INF/faces-config.xml … Read more

Changing JSF prefix to suffix mapping forces me to reapply the mapping on CSS background images

and then I see that everything going through FacesServlet has .xhtml appended to it, so that the browser is requesting .png.xhtml files, .css.xhtml file – is this right? This only applies to resources included by <h:outputStylesheet> and <h:outputScript>. This is not related to the change in the URL mapping. This is related to the change … Read more

How to insert special characters like & and < into JSF components' value attribute?

You need to escape them to XML entities. <h:outputText value=”Tom &amp; Jerry Show” /> This problem is not related to JSF per se, but to the view technology. You’re apparently using Facelets (which is perfectly fine!). Facelets is however a XML based view technology, so you would need to ensure that the template is well … 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

Error Parsing /page.xhtml: Error Traced[line: 42] The entity “nbsp” was referenced, but not declared

Facelets is a XML based view technology. XML has only five predefined entities. The &nbsp; is not among them. It works only when used in plain HTML or in legacy JSP (note: it doesn’t work in JSPX as that’s also XML based!). To fix this, you either need to declare the entity yourself in the … Read more