JSF, Facelets, Myfaces and Richfaces Performance Tuning (转载)
I often come across the same issues in projects. One very hot topic is performance.
Beside the things you should do to garantee performance during the development process,
you are often left alone with tunings for frameworks. Good examples are the widly used JSF
frameworks Myfaces from Apache and Richfaces from JBoss.
I tried to sum up some of our performance findings during the last profiling and tuning sessions.
Happy to hear your ideas and best practices on that, too.
Myfaces General Performance Tuning
If you use the MyFacesExtensionsFilter you can start optimizing here.
It buffers and parses the response on every request.
You can disable this, and still gain all functionality the ExtensionsFilter is providing,
by doing the following.
If you use myfaces the traditional way, you end up having all resources needed for your components
beeing loaded in separate includes. You could optimize this using the following context param:
<context-param>
<param-name>org.apache.myfaces.ADD_RESOURCE_CLASS</param-name>
<param-value>org.apache.myfaces.component.html.util.StreamingAddResource</param-value>
</context-param>
The only thing to do now is get rid of the <HEAD> tag in your HTML, and instead use Tomahawk's <t:documentHead/> tag.
Of course, your <f:view/> tag has to enclose your <t:documentHead/> tag for this to work. Now
all resources needed by your components get loaded in one single file.
Think about using an optimized sirialization provider. (Following example is for JBoss)
<context-param>
<param-name>org.apache.myfaces.SERIAL_FACTORY</param-name>
<param-value>org.apache.myfaces.JbossSerialFactory</param-value>
</context-param>
Server Side State Performance Tuning
There are a many settings you can enable in your web.xml file to make MyFaces perform well.
JSF components tree state takes big enough memory. In the server-side
state saving ( default JSF behavior ) these objects are stored in the
session. For a many concurrent user connections every user gets own
session object.
In general serverside state saving is more performant than the client side. You also get lower
page sizes and faster loading times with this.
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
If your performance is ok, but you have memory problems try to switch to the client-side state saving.
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
If this is not enough, you could disable serverside state compression. Compression always
takes time. If you have enought memory, try this one:
<context-param>
<param-name>org.apache.myfaces.COMPRESS_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
Very important, too, is to disable the serialization of state, serialization and deserialization of the component tree is a major performance hit.
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
If you find that memory is a constraining factor, then reducing the number of views stored in the session might help. The setting is controlled by:
<context-param>
<param-name>org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION</param-name>
<param-value>20</param-value>
<description>
Only applicable if state saving method is "server" (= default).
Defines the amount (default = 20) of the latest views are stored in session.
</description>
</context-param>
Sun-RI Performance Tuning
You should increase response buffer (to reduce reallocations at render time)
<context-param>
<param-name>com.sun.faces.responseBufferSize</param-name>
<param-value>500000</param-value>
</context-param>
Think about using an optimized serialization provider. The following example is a JBoss provider (code here)
)
<context-param>
<param-name>com.sun.faces.serializationProvider</param-name>
<param-value>org.jboss.web.jsf.integration.serialization.JBossSerializationProvider</param-value>
</context-param>
Facelets Performance Tuning
If you don't want to store too many state in either the server or the client, another possible
solution is to allow Facelets to build view before request processing instead of state saving.
But be warened about some unpredictable side effects. Use web.xml init parameter
together with the attribute.
<context-param>
<param-name>facelets.BUILD_BEFORE_RESTORE</param-name>
<param-value>true</param-value>
</context-param>
The ultimate solution for solving session state memory or performance problems is to create a custom
aceletsViewHandler subclass with special state handling where needed.
The custom handler could , for example, call buildView method instead of real restoreView
procedure for pages that typically do not need state (e.g. menue, navigation).
Facelets library in the "debug" mode stores information about
components and beans up to 5 times for an every user. You should disable this in production mode!
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
You should increase response buffer (to reduce reallocations at render time)
<context-param>
<param-name>com.sun.faces.responseBufferSize</param-name>
<param-value>500000</param-value>
</context-param>
You should also turn of the facelets refresh trigger in production environments.
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>-1</param-value>
</context-param>
RichFaces Performance Tuning
Most filters use buffering for request processing. According to the
profile information, these buffers took big enough memory in the
application. Therefore you should limit the size for the RichFaces Ajax filter, too.
<init-param>
<param-name>maxRequestSize</param-name>
<param-value>100000</param-value>
</init-param>
For a production server, it makes sense to reduce the value to a real page
size or even remove that parameter at all.
Richfaces comes with some build in parsers to ‘tidy’ all HTML HTTP Responses so that they are valid
XHTML (thus XML compliant). This is needed as dynamic DOM updates in the browser need correct XML.
Of course, parsing HTML incurs a performance overhead.
This can be minimized by setting the forceparser setting to false. In that case only AJAX responses will be ‘tidied’. In the other case all JSF responses are ‘tidied’.
<filter>
<filter-name>richfaces</filter-name>
<display-name>RichFaces Filter</display-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
<init-param>
<param-name>forceparser</param-name>
<param-value>false</param-value>
</init-param>
</filter>
TIDY xml filter is DOM-based, thus it requires a lot of memory. It
would be better to use more optimized "NONE", "NEKO" ore "TIDY" should be the second and third best choice. The following example shows that ORDER parameter defines the order in which particular filter types are used for pages code correction.
<context-param>
<param-name>org.ajax4jsf.xmlparser.ORDER</param-name>
<param-value>NONE,NEKO,TIDY</param-value>
</context-param>
You can even set sets of pages for which the filters should apply. The following example applies the NEKO filter to all URLs.
<context-param>
<param-name>org.ajax4jsf.xmlparser.NEKO</param-name>
<param-value>.*\..*</param-value>
</context-param>
Before the version 3.1.3, RichFaces loaded styles and script on demand. I.e. files are loaded only if they are required on a particular page. Since RichFaces 3.1.3, it's possible to manage how the RichFaces script and style files are loaded to application.
Using the web.xml org.richfaces.LoadScriptStrategy setting, you can tell Richfaces to either:
* Load ALL script in one file.
* Load NONE scripts (you do it yourself instead - eg. in the manner prescribed by your book).
* Load scripts when needed (the DEFAULT).
<context-param>
<param-name>org.richfaces.LoadStyleStrategy</param-name>
<param-value>ALL</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name>
<param-value>false</param-value>
</context-param>
If you use LoadScriptStrategy ALL, turn the compression off like it shown in the code snippet above
Some basic JSF performance principles
Never put logic into your getters. They are called multiple times and should only return something already populated by another method. For example if you are chaining drop-downs together use an a4j:support tag on the first one with an action attribute that loads the data which is then retrieved when you reRender the second one.
Use the ajaxSingle="true" unless you actually want to send the whole form back to the server.
Don't use a rich component if you only need a normal one. For example don't use rich:dataTable unless you are making use of some of the features that it has over and above h:dataTable.
Consider using immediate=true attributes on elements where you do not need validation
Avoid displaying large tables to user. Use pagination
Do not over complicate EL expressions, code them in Java in backing bean
分享到:
相关推荐
在开发过程中,你可以利用MyFaces和RichFaces提供的组件来快速构建界面,同时利用Facelets的模板和复合组件功能来组织和重用视图代码。这些包的组合使用,可以帮助你构建出功能强大、交互性强的Web应用程序。
3. **组件库支持**:Facelets与多种JSF组件库(如MyFaces、RichFaces等)无缝集成,为开发人员提供了丰富的UI组件选择。 4. **编译时检查**:与JSP相比,Facelets在部署时进行编译,这有助于提前发现潜在的错误,...
5. **Integration with other JSF Libraries**: RichFaces可以与其他JSF库无缝集成,如PrimeFaces、MyFaces等,以扩展功能或优化性能。 在实际开发中,`richfaces-ui-3.2.0.SR1`的使用可能涉及到以下步骤: - 添加...
4. **示例代码**: 学习和分析开源的JSF项目,如Mojarra(JSF参考实现)和MyFaces。 通过以上内容,你可以开始你的JSF学习之旅,掌握这个强大的Web开发框架。记住,实践是最好的老师,动手操作和不断探索是精通任何...
例如,MyFaces和RichFaces是两个流行的JSF扩展库,提供了更多的组件和功能。 **10. JSF与其它技术集成** JSF可以与其他Java EE技术如EJB、CDI、JPA等无缝集成,构建完整的企业级应用。同时,JSF也支持与Spring框架...
RichFaces 4是其一个重要的版本,引入了更多的AJAX功能和性能优化。 在**JSF2**中,主要的改进包括: 1. **Faces Flow** - 为应用程序提供了更强大的导航控制,支持更复杂的流程。 2. **Partial State Saving** - ...
它基于 Mojarra 实现,不支持 Myfaces。 与标准复合组件不同,它: 不会在 JSF 组件树中创建新的组件节点允许将非组件标签包装为 <p> 或转换器Tagext 库的主要目的是以与 JSF 复合组件相同的方式围绕 JSF 组件进行...
- **Facelets支持:** 讲解了如何在RichFaces中启用和利用Facelets模板引擎。 - **JBoss Seam支持:** 针对与JBoss Seam框架的集成进行了详细介绍,包括配置和最佳实践。 - **Portlet支持:** 介绍了如何在Portlet...
然而,由于Facelets提供了更好的可维护性和性能,现代JSF应用通常倾向于使用Facelets。 在实际开发中,JSF可以通过以下方式提高效率: 1. **组件库**:如PrimeFaces、MyFaces和RichFaces等第三方组件库提供了大量...
类似的还有RichFaces、IceFaces等,它们为JSF带来了更丰富的用户界面元素。 7. **CDI (Contexts and Dependency Injection)**: CDI(如`javax.enterprise.cdi-api.jar`和`weld-servlet.jar`)是Java EE中的依赖...
- **支持的JavaServer Faces实现和框架**:为了确保与JSF 的良好集成,RichFaces 需要与特定的JSF 实现和框架进行配合使用,例如Sun JSF RI 和Apache MyFaces。 - **支持的服务器**:RichFaces 3.2 可以在多种应用...
- `myfaces.jar` 或 `richfaces.jar` 等:可能是特定实现或扩展库,如MyFaces或RichFaces,提供了额外的功能和组件。 通过导入这些Jar包到项目中,开发者可以直接使用JSF提供的功能,快速构建功能丰富的Web应用。...
JSF 2.x引入了 Facelets、Ajax 支持和Managed Bean注解等功能,JSF 3.x则继续优化性能,支持更多的新特性,如响应式设计和WebSockets。 通过这个基础教程,你将对JSF有全面的认识,包括其核心概念、组件系统、生命...
RichFaces在不同环境下可能需要特定的配置,如Web应用描述符参数、Sun JSF RI、Apache MyFaces的支持、Facelets集成、JBoss Seam集成、Portlet支持等,这些都对框架的功能扩展和性能优化至关重要。 #### RichFaces...
对于采用Sun JSF参考实现或Apache MyFaces的项目,指南详细阐述了如何针对这两种框架进行优化设置,确保RichFaces组件的高效运行。 ### 3. Facelets与JBoss Seam支持 此外,Facelets模板引擎和JBoss Seam框架的集成...
根据给定的信息,本文将对JSF框架及RichFaces组件库进行深入解析,重点介绍其技术要求、安装步骤以及核心概念等内容。 ### 技术要求 #### 支持的Java版本 - Java SE 5 或更高版本:由于JSF框架是基于Java标准版...
- 阐述如何调试JSF应用,解决常见问题,并进行性能优化。 总结:《JSF实战》一书全面覆盖了JSF技术的各个方面,从基础概念到高级特性的应用,旨在帮助读者快速掌握并熟练运用JSF进行Web开发。通过阅读本书,开发者...
- Facelets 是一种用于构建 JSF 应用程序的视图层技术,RichFaces 提供了对该技术的支持。 5. **JBoss Seam 支持:** - JBoss Seam 是一款基于 JSF 的企业级应用程序框架,RichFaces 与之有良好的集成。 6. **...