31 July 2009

JSF custom properties file

When building web apps in JSF, you often need some global values, such as server paths, email addresses, messages, etc. Hardcoding these in your pages doesn't make a whole lot of sense; the values might change over time or from server to server. Also, cluttering up the built-in JSF properties files isn't the most ideal solution.

The answer is to use custom properties files and load them using the loadBundle tag. Say you've created a properties file called CustomProps.properties in the package com.companyname.resources. In your JSF page or your template file, use the following to load the file:
<f:loadBundle basename="com.companyname.resources.CustomProps" var="props" />
If you have a property called hellomessage in the file, you reference it using the props variable:
<h:outputText value="#{props.hellomessage}" />

21 July 2009

Referencing an object in JSF/Richfaces

With JSF 1.2, referencing objects in the component tree isn't as straightforward as it used to be. The findComponent() method has been deprecated, so we must use other means to find an object. Jacob Hookom and Ric Smith provide slight variations on how to accomplish this task. Both involve using the ContextCallback object; here's a snippet to give you an idea:
public static final ContextCallback RENDER = new ContextCallback() {
public void invokeContextCallback(FacesContext ctx, UIComponent c) {
c.renderAll(ctx);
}
};

boolean found = root.invokeOnComponent(faces, clientId, RENDER);

if (!found) throw new FacesException(clientId + " not found!");

Note in the above code, you use the invokeOnComponent() method to (what else?) invoke a particular action on the component in question. Note that you need to use the client ID for the object, which in JSF looks something like this: myForm:myComponentID

In turn, the RENDER method calls invokeContextCallback(), which carries out some action on the component. This can be handy for programmatically adding controls to the page at a specific location; c.renderAll(ctx) can be replaced with whatever you need.

You can also add objects to the page using Max Katz' technique, though I'm not sure if that way you're allowed to put them at a specific location in the component tree.

16 July 2009

Turn off Flash player debug messages

If you use the Flash debugger version in your browsers, you often see the AS3 debug window on sites around the Internet. Anytime an exception is encountered, the debug window pops up. This happens almost daily with the MSNBC site.

There's an easy way to disable the debug messages without uninstalling the Flash debug player; these instructions are for Windows, though the same concept applies to the other platforms.
  1. Go to C:\Documents and Settings\[your_login_name]
  2. Open/create the file mm.cfg.
  3. Add the following to the file:
    SuppressDebuggerExceptionDialogs=1
If you need to re-enable the debugging, change the 1 to a 0.

03 July 2009

02 July 2009

JSF- Disable Submit button, re-enable if validation fails

Ran into an interesting question: How do you disable a Submit button when the user clicks it, but re-enable it if validation fails? With JSF, it's actually simple. Here's an example with Richfaces:
  onclick="this.disabled=true"
oncomplete="#{facesContext.maximumSeverity == null ? '' : 'this.disabled=false'}"/>

Disable the Submit button on every click; then oncomplete, see if there were errors. If so, re-enable the Submit button.

01 July 2009

What is "infoviz?"

This great article from Amanda Schaffer describes the concept of information visualization, "infoviz" for short.
"Display an unwieldy mass of data in clever visual form and you may gain ├╝ber-insight into questions you hadn't yet put into words... The field has long helped scientists, engineers, and businesspeople see the unseen as it emerges from complex data: Users may spot promising molecules for pharmaceutical testing, for instance, or pinpoint glitches in a supply chain."