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

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

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

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

@Inject to pass params to a CDI @Named bean via URL

This works only with the in JSF 2.3 introduced javax.faces.annotation.ManagedProperty. @Inject @ManagedProperty(“#{param.id}”) private String id; The now deprecated javax.faces.bean.ManagedProperty annotation works only in JSF @ManagedBean classes. I.e. in instances which are managed by JSF. It does not work in instances which are managed by CDI @Named. Further, you’ve made another mistake: you’re trying to prepare … Read more

How to invalidate an user session when he logs twice with the same credentials

The DB-independent approach would be to let the User have a static Map<User, HttpSession> variable and implement HttpSessionBindingListener (and Object#equals() and Object#hashCode()). This way your webapp will still function after an unforeseen crash which may cause that the DB values don’t get updated (you can of course create a ServletContextListener which resets the DB on … Read more