JSF Best Practices

SHARE & COMMENT :

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

Open Source Components

    • Use myfaces . It doesn’t matter which one you use because both comes from Sun. Sun offers myfaces through open-source community.

also read:

IDE

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

Naming Convention

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

Techniques

  • For bigger project is better to extend all the default components, that gives flexibility and extensibility to add new features.
  • Use components effectively like oops concept to minimize the coding and maximize the usage.
  • 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.
  • Use Map object to capture values for components created inside <c:foreach> loop.
  • Write a custom datetime converter and register with java.util.Date type to keep control of TimeZone and default date format.
  • Customize default JSF error messages. See here.
  • Use facelets layout feature to simulate Struts Tiles kind of behavior
  • Always use Wrapper classes instead of primitive types. If you use primitive type passing empty values from the field will through exception.
  • Use <h:outputLink> for simple page navigations. <h:commandLink> submits form and does validation too.
  • 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.
  • 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.
  • 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.
  • If you using JSF1.2, use field specific custom error message wherever is required. Example:
     <h:inputText value="#{bean.value}" requiredMessage="#{bundle.requiredMessage}"
      converterMessage="#{bundle.converterMessage}" validatorMessage="#{bundle.requiredMessage}">
      

    More information can be found here.

  • 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

  • Add immediate=”true” for Cancel and Reset type of buttons to bypass validations.
  • One of the major missing feature in JSF is calling an action before page invocation.Here is the workaround.
  • Here an work around solution explained to invoke an action from url.
  • This link explains the difference between <c:forEach> and <ui:repeat>
    http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets
  • 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.
  • 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.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better