JSF: PrimeFaces: Basic Custom Composite Component

The following describe how to create a basic custom composite component in JSF 2.0+ for Primefaces. The method of using rendering classes, back beans, type classes, and others are not included in this tutorial. Please note that the IDE used for programming is NetBeans 7.4

 

  1. Create a Folder
    Create a subfolder into the resources folder as show in the following image. In this case, we call this folder “utils”.
    create_folder
  2. Create a XHTML File
    Inside the folder utils, lets cretae a XHTML file. This file will contain our custom composite component.
    In this case, we call this file “displaySuperValue.xhtml”
  3. File “displaySuperValue.xhtml” Content
    Copy and Paste this code example:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"	  	 
    	 xmlns:h="http://xmlns.jcp.org/jsf/html"
    	 xmlns:composite="http://java.sun.com/jsf/composite">
    	<head><title>Show Super Value</title></head>
    	<body>
    
    	  <composite:interface>
    	    <composite:attribute name="supervalue" required ="true" />
    	  </composite:interface>	
    
    	  <composite:implementation>			
    	    <hShockutputText value="This is SuPeR: #{cc.attrs.supervalue}" />
    	  </composite:implementation>
    
    	</body>
    </html>
  4. About Your Custom Component
    At the beginning of your file, you will need to include the composite namespace if the IDE did not include it automatically. Just add the following line:

    xmlns:composite=”http://java.sun.com/jsf/composite”

    While the interface tag should hold all the attributes of your custom tag, the implementation tag should hold all your web interface script. In order use the attributes declared inside the interface tag, you need to use the “cc” bean provided by JSF. This bean is based on the UIComponent class.
    cc bean

  5. Implementing Your Custom Composite Component
    Go to the XHTML file which you wish to use your custom composite component.
    Start typing as you normally would do when writing a JSF tag by typing the name of the folder you created under the resources folder as show below:
    listWhile “utils” is the name of the folder, “displaySuperValue” is the name of the XHTML file you created.
    As you keep typing, when using the space, the NetBeans IDE will display a list of all the default attributes including those you created inside the interface tag. Normally, the NetBeans IDE should include the namespace that will allow you to access to your custom component; however, if the IDE doesn’t include the namespace, just add the following namespace and the problem should be solved:

    xmlns:utils="http://xmlns.jcp.org/jsf/composite/utils"
Share

NetBeans: PrimeFaces: Adding Theme to Project

Adding a theme to your PrimeFaces is simple:

  1. Go to the PrimeFaces theme Showcase Labs so you can pick which theme you wish to use (remember the name of the theme):
    http://www.primefaces.org/showcase-labs/ui/home.jsf?theme=blitzerprimefacespagetheme
  2. Go to the PrimeFaces theme repository: http://repository.primefaces.org/org/primefaces/themes
  3. Using the name of the theme you wish to use, go inside the theme folder and then go into the version you wish to use.
  4. Download the jar file anywhere you wish
    primefacespagethemefolder
  5. Open NetBeans and go into your project
  6. Do right-click “Libraries” folder and select “Ad JAR/Folder”
    NetBeans_Libraries_AddJar
  7. Select the JAR file
  8. Your jar file should show up under the Libraries folder
  9. Next, go into the folder WEB-INF and open the file web.xml
  10. Add the following xml code into this file
    <context-param>
    	<param-name>primefaces.THEME</param-name>
    	<param-value>blitzer</param-value>
    </context-param>
  11. Run and/or deploy the website. Done!
Share

Groovy Tutorial – Part 1

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Note: If you are not interested to know about the differences between Groovy and Java, skip to part 2.

Groovy is a dynamic language (which means many behaviors are executed at runtime instead of during compilation) based on the Java Virtual machine. This means that Groovy is tightly integrated with the Java language.

The biggest differences between Groove and Java are:

  1. The following packages and classes are imported by default (no need to use an explicit import to use them):
    • java.io.*
    • java.lang.*
    • java.math.BigDecimal
    • java.math.BigInteger
    • java.net.*
    • java.util.*
    • groovy.lang.*
    • groovy.util.*
  2. These are optionals:
    • Semicolons ‘;’ are optional
    • The keyword ‘return’ is optional
  3. The keyword ‘this’ can be used inside static methods.
  4. Since in Groovy there is no difference between checked and unchecked exceptions, the clause ‘Throw’ in a method signature is not checked
  5. There is no compile errors for using:
    • Undefined members
    • Passing Arguments of the wrong type
  6. The == operator and the method is()
    • The operator == will check for equality (checking if both object are equal) instead for identity (checking if both variables point the same object)
    • The method is() allows for checking for identity.
      • Example: foo.is(bar)
      • This doesn’t work on null. For checking null, you can still using == operator
  7. New features added to Groovy not available in Java:
    1. Closures: piece of code which is defined and then executed at a later; also known as “code block” or method pointer.
    2. Lists and maps  are native syntax which means that Groovy threads them as first class construct.
    3. Groovy provide native support for regular expressions
    4. Dynamic and static typing is supported
    5. Expressions can be embedded inside strings
    6. GPars (Groovy Parallel Systems) framework support for parallel processing. It supports:
      • Actors
      • Map and Reduce
      • Fork and Join
      • Dataflow
    7. GPath (Path Expression Language): Allows parts of nested structured data to be identified.
    8. GroovyMarkup: Native support for markup languages such as XML, HTML, W3C DOM, Ant Tasks, SAX, Swing, etc.
    9. Polymorphic interaction: Similar to STL standard iterators with these following differences:
      • Declarations wrapped in traversable template which makes the collection accessible
      • Constant iterator declaration is not scoped by the collection class. Instread the collection is passed in as a constructor argument.
    10. Switch statement:
      • In java, we can only use the int or int-derived type classifier.
      • In Groovy, we can any classifiers for a switch statement.
    11. Static and dynamic typing are supported.
      • If you wish to use a dynamic types variable you can use the keyword ‘def’
    12. Writing beans with simpler syntax for both properties and adding even listeners.
    13. Simplified way to check null which eliminate nasty nested ifs
    14. All variables are reference to objects. Groove doesn’t use primitive variables.
      • Even do you can write a primitive type as a short form, Groove will always translate it into the object.

Part 2 comming up soon…

Share