FacesMessages Basics

In your XHTML Page:


	
	Name: 
	

In your backing bean:

// to show up where globalOnly messages appear
facesMessages.add("Changes were not applied...");

// do your field-level validation
facesMessages.addToControl("name", "Name was not valid.");

Results:
Picture 2.png

Update your CSS to fit your style preferences.

Syntax Highlighter

Just installed the Syntax Highlighter from Alex Gorbatchev, and like it quite a bit… here’s an example.

public int factorial( int number ) {
  if( number == 1 ) {
    return 1;
  }
  return number * factorial( number - 1 );
}

The key is to use a name=”code” and class=”java” (or xml, or others…) in your pre-tag. Add the javascript calls required for the feature, and your code shows up nicely on the web page…

For easy copy and paste:

<pre name="code" class="java">

</pre>

Nesting Seam Validation Tags

Here’s an interesting one that caused me to stumble for a while… If you are leveraging Seam’s field-level validation and are mixing it with Rich Faces components, you have to be conscious of how you nest your tags.

For example, this caused validation to be skipped:



    
        
            
                
            
            
                
            
        
        
    

When clicking on the save button, you will bypass validation and go directly to the save() method on the named component myService.

To fix this, you need to nest the tags within each tab panel:



        
            
            
                
            
            
            
            
                
            
            
        
        

Viola, now your required tags are properly enforced, as well as any Hibernate annotation validation that exists.

JSF Tricks

Here are a few tricks over at the MyFaces Wiki:

  • Executing an action when a drop-down value changes
  • Executing an action when the enter key is pressed

The latter is helpful when the Ajax4JSF components stop the enter key from submitting a form / executing an action.

The rest of the wiki is a little bit sparse, but still has some good topics.