Programming/JSF
JSF Best Practices
Jared
2008. 7. 3. 19:24
Open Source Components
- Use myfaces or Sun RI. I feel it doesn’t matter 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.
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.
- Don’t create managed beans in session scope. Always create in request scope.
- 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}”>
Work around for some common issues
- Add immediate=”true” for Cancel and Reset type 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 <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.