JSF Best Practices
MAY 20, 2008 45 COMMENTS
Here I am documenting some best practices can be followed during JSF project development.
Open Source Components
- 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.
- Use richfaces to get rich look quicker. Click here to go to demo site.
- Use facelets to avoid compilation time and better code readability.
- 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.
- 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.
- For bigger project it is better to extend all the default components, that gives flexibility and extensibility to add new features in future.
- 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
- 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.
- 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 data type passing empty values from the field will through exception.
for simple page navigation, use to submit 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.
instead of rendered attribute for and . If you use rendered attribute sometime action may not be fired if the bean by default returns false.
- 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.
- If you are using JSF1.2, use field specific custom error message wherever is required. Example:
More information can be found here.
- Don’t use
inside iterative tags like and . see here to know the reason.
Work around for some common issues
- Add immediate=”true” for Cancel and Reset types of buttons to bypass validations. See here.
- 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
- 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.
- With JSF implementation submitting form with enter key will not invoke action method. Here is a custom component which will solve this problem.