Why not JSF pages are precompiled (atleast partial

2019-01-17 04:56发布

I don't know whether it is true or not but from what I've read, I believe, JSF EL & xhtml pages are not pre-compiled & just used when needed but instead they are parsed, evaluated, compiled each time the view is built.

I fail to understand why this is done so! Why not just parse & compile it just once, ok atleast partially , rendering of some components may depend on a dynamically fetched variable so they may be rendered later but why delay that for all the components on page? Whatever maximum could be pre-compiled & made ready to use, why not do it just when the application is deployed? Wont this improve rendering time of the pages ?

1条回答
贪生不怕死
2楼-- · 2019-01-17 05:42

Facelets is actually capable of "precompiling". You can control the Facelets refresh period with the context parameter javax.faces.FACELETS_REFRESH_PERIOD. You can set it to -1 in order to tell JSF to never re-compile/re-parse the Facelets files and actually hold the entire SAX-compiled/parsed XML tree (based on the XHTML compositions) in cache:

<context-param>
    <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
    <param-value>-1</param-value>
</context-param>

Don't use this setting during development though, or you must restart the whole server on every edit of a Facelets file. Mojarra has a default setting of 2 (meaning, cache will be refreshed every 2 seconds). MyFaces has a default setting of -1 when javax.faces.PROJECT_STAGE is not set to Development.

You can if necessary control the Facelets cache by providing a custom FaceletsCacheFactory and FaceletsCache. Note that this is only available since JSF 2.1 and thus you'd need to redeclare your faces-config.xml conform JSF 2.1 in order to get <facelet-cache-factory> configuration setting to work.

To get a step further, the views which are built based on the XML tree (so, the entire UIViewRoot) could theoretically also be pooled. MyFaces is currently already making some effort in order to achieve this, see also issue 3664. My fellow Arjan Tijms is in his spare time also looking at it for Mojarra.

查看更多
登录 后发表回答