Posts

Actions not being called when using 2 view tags

I had a problem where a command button's action was not being called on the managed bean. When using tiles inside JSF, you may have it set up so that you have a template.jsp and a body.jsp. The template.jsp, using tiles inserts body.jsp. template.jsp defines a <f:view> tag as per normal. Inside body.jsp, whilst you don't technically need a <f:view> tag, you may get some component (namely f:phaseListener ) complaining about not having a view root, so you does it asks and defines the <f:view> at the start of the file. You load up the page, everything renders correctly and you click a submit button. The form validation errors appear as normal, so you fill in some values and click submit again. This time, NOTHING. So you try turning on all the logging and still nothing seems wrong. This happened to me. I wasted quiet a few hours on this one: working out how to log things correctly using eclipse/tomcat and get Java Util Logging working etc. After a ton of breakp...

Calling JSF actions by URL

In JSF 1, there is no provided functionality to invoke an action (by this I mean a java function, not a JSF action invoked by a component). JSF 2 adds functionality to support this, through f:viewParam / f:event and the PreRenderViewEvent . However in JSF 1, you can bind a phase listener to a specific page using the f:phaseListener tag to call the code before the Render Phase. To make sure that the code is only called when the page is access via the url and not from a postback caused by a component you can use the function added in JSF 1.2:  ResponseStateManager.isPostback(FacesContext context) . First off we configure the phase listener in the page (action.xhtml): <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"> <f:phaselistener binding="#{urlActionBean.listener}" /> Next is the phase listener and action code: public class UrlActionBean { /*...

Eclipse Plugins

After having my elcipse install crashing several times, I thought it a good idea to compile a easily accessible list of plugins I use. Log4E Allows loggers to be added to classes, replaces System.out and printStackTrace etc Website: http://log4e.jayefem.de/content/view/3/2/ Update site: http://log4e.jayefem.de/update Log4Eclipse Log4j socket appender host - allows log4j to send log messages directly to eclipse (no need to inspect log files). I have rebuilt it using log4j 1.2.15 see below. Website: http://www.nitwit.de/log4eclipse/ My build: log4eclipse_0.4.0.jar Subclipse Subversion client Website: http://subclipse.tigris.org/ v1.6 Update site: http://subclipse.tigris.org/update_1.6. x v1.8 Update site: http://subclipse.tigris.org/update_1.8.x JBoss Tools Tools for JBoss server and JSF/Richfaces/Facelets editors. Plus a lot of others (I just use the richfaces plugin). Website: http://www.jboss.org/tools Update site: http://download.jboss.org/jbosstool...

Multiple Log4J instances in a WebApp

Many times I've come across the classic problem of different logging instances in an web app server. Most of them involve classloader conflicts: The webapp has log4j.jar but the webapp has bundled log4j.jar as well and you get errors. OR you have the log4j.jar in the application server's shared library directory and you have 2 webapps but the configuration conflict - or even just a configuration for the application server itself and one for the webapp. I've finally come across a solution for log4j. It involves using a repository selector based on a context, this is fully described here - Supporting the log4j RepositorySelector in Servlet Containers . So the solution to stop conflicts and independently configure each application is this. First put ALL logging jars in the shared library directory including ( log4j.jar, slf4j-api.jar, slf4j-log4j.jar, commons-logging.jar, etc). This ensures that if they one of the libraries is loaded in the main classloader it can access the ...

Access Control in JSF using a PhaseListener

After doing a quick search of the web, I did not find any nice solutions to implementing access control in JSF. Using a servlet filter mapping seemed inadequate, and there wasn't any obvious place to start. I initially tried using a custom NavigationHandler, however that is only used after an action is performed (e.g. #{someBean.action} ), and not for directly accessing a URL (e.g. typing in /test.faces). Some more searching revealed that a PhaseListener was the place to do it. After checking the JSF lifecycles I determined that RESTORE_VIEW was the correct place to do it - ALL pages go through at least the RESTORE_VIEW and RENDER_VIEW phases. You can check the viewId in the afterPhase (as the view has been loaded in this phase, hence can't check in beforePhase) and redirect using the navigation handler as nesecary. Below is my implementation of it. I used a flexible inclusion/exclusion filter so I can make the rules as complex as I want. This implementation determines ...

JSF - Injecting Managed Beans into Managed Beans

Using Google Guice and JSF with something like GuiceSF to inject the JSF managed beans (backing beans) gives you a lot of power over the injection. However, by doing so JSF no longer resolves the EL expressions from faces-config.xml and injects them into the managed beans. An easy way around this, and somewhat cleaner, is to create an annotation that is 'bound' to an EL expression. To do so requires the use of GuiceyFruit's extensions to Guice. First create the annotation: import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Retention; import java.lang.annotation.Target; import com.google.inject.BindingAnnotation; /** * Injection by expression language. * @see com.google.inject.name.Named */ @BindingAnnotation @Target( { FIELD, PARAMETER, METHOD }) @Retention(RUNT...

EJB3 Stateful Session Beans with ejbCreate

I had trouble obtaining a stateful session bean using the EJB3 pattern when trying to pass initial data to it. You can use injection to create instances of stateful beans by simply declaring an instance variable in a bean, for example: @EJB MyStatefulLocal mylocal; . But this doesn't allow you to pass any data on creation. The answer is to use a EJBLocalHome interface like in EJB2 but it's a bit complicated with all the annotations. The ejbCreate method from EJB2.1 is called create in the local home interface and called whatever you want in the bean itself (as long as it is annotated with @Init Below is an example. You can download the source here: download source . /** Local Interface. Dummy interface to enable us to return the Business interface. */ public interface MyStatefulLocalEjb2 extends EJBLocalObject, MyStatefulLocal {} /** Local Business Interface. Put business method definitions here */ public interface MyStatefulLocal {} /** Define method(s) which create the E...