Tuesday, December 31, 2013

JSF Best Practices

Here I am documenting some best practices can be followed during JSF project development.

Open Source Components

  1. Use myfaces or Sun RI. It doesn’t matter which one you use because both comes from Sun. Sun offers myfaces through open-source community.
  2. Use richfaces to get rich look quicker. Click here to go to demo site.
  3. Use facelets to avoid compilation time and better code readability.

IDE

  1. Use eclipse latest version
  2. Use richfaces eclipse plugin. It offers Visual Page Editor for Richfaces, JSF, and HTML / CSS.
  3. Use Tomcat instead of JBoss to avoid server restart for each changes.

Naming Convention

  1. Keep resource bundle name and xhtml name as same. Example SearchUsers.xhtml and SearchUsers.properties
  2. Suffix all the managed bean with “Bean” word, like SearchUsersBean.java
  3. Keep all the folder names in lower case.

Techniques

  1. For bigger project it is better to extend all the default components, that gives flexibility and extensibility to add new features in future.
  2. Use components effectively like oops concept to minimize the coding and maximize the usage.
  3. To keep your application scalable, minimize creating managed beans in session scope. Many of them recommending session scope for managed beans to solve the JSF component state problem but it is a serious issue if you want to host your application in clustered environment.
  4. Use Map object to capture values for components created inside loop.
  5. Write a custom datetime converter and register with java.util.Date type to keep control of TimeZone and default date format. See here for more information.
  6. Customize default JSF error messages. See here.
  7. Use facelets layout feature to simulate Struts Tiles kind of behavior.
  8. Always use Wrapper classes instead of primitive types. If you use primitive data type passing empty values from the field will through exception.
  9. Use for simple page navigation, use to submit form and does validation too.
  10. Don’t hesitate to use simple HTML input controls where JSF doesn’t provide good solution. For example displaying radio button in datatable has issues when you fetch data from database. For these scenarios I prefer to use simple HTML controls.
  11. Use instead of rendered attribute for and . If you use rendered attribute sometime action may not be fired if the bean by default returns false.
  12. If you are using JSF1.2, use to execute all your page initialization stuffs like fetching data from database, creating error messages to be displayed.
  13. If you are using JSF1.2, use field specific custom error message wherever is required. Example:

    More information can be found here.
  14. Don’t use inside iterative tags like and see here to know the reason.

Work around for some common issues

  1. Add immediate=”true” for Cancel and Reset types of buttons to bypass validations. See here.
  2. One of the major missing feature in JSF is calling an action before page invocation. Here is the workaround.
  3. Here an work around solution explained to invoke an action from url.
  4. This link explains the difference between and
    http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets
  5. If you need to redirect from navigation-case to URL with param which is a property of a managed bean. Here is the work around.
  6. EL expressions in JSF are executed multiple times for datatable and other iteration tags hence get method in managed bean might call multiple times. Either initialize your variables in action method or @PostConstruct method or follow lazy initializationRead here for more explanation.
  7. With JSF implementation submitting form with enter key will not invoke action method. Here is a custom component which will solve this problem.
add to del.icio.us :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine ::  ::  :: TailRank :: post to facebook

Related Articles

No comments: