JSF Best Practices.

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

Open Source Components


  • 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.


  • 1. For bigger project is better to extend all the default components, that gives flexibility and extensibility to add new features.
  • 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 <c:foreach> loop.
  • 5. Write a custom datetime converter and register with java.util.Date type to keep control of TimeZone and default date format.
  • 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 type passing empty values from the field will through exception.
  • 9. Use <h:outputLink> for simple page navigations. <h:commandLink> submits 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 <c:if> instead of rendered attribute for <h:commandLink> and <h:commandButton>. If you rendered attribute sometime action may not be fired if the bean by default returns false.
  • 12. If you using JSF1.2, use <f:view beforePhase=”#{method-binding}”> to execute all your page initialization stuffs like fetching data from database, creating error messages to be displayed.
  • 13. If you using JSF1.2, use field specific custom error message wherever is required. Example:
  • 14. Don’t use <c:if> inside iterative tags like <h:datatable> and <ui:repeat>. see here to know the reason.

Work around for some common issues

  • 1. Add immediate=”true” for Cancel and Reset type of buttons to bypass validations.
  • 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 <c:forEach> and <ui:repeat>
  • 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 initialization. Read here for more explanation.

This article is originally published in Venkat Sadasivam’s Blog. Republished here with
author’s permission.

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注