JSF Best Practice: Custom Components and JavaScript

喜你入骨 提交于 2019-12-01 18:34:01
Xtreme Biker

If you want your components to be reusable, I encourage you to pack everything in an independent jar. If using Servlet 3.0, you'll be able to easily access the web resources putting them in META-INF/resources. Provide the jar a faces-config.xml and you'll make it JSF annotation scannable:

components
    \-(Your cource code)
META-INF 
    \-faces-config.xml
    \-resources (This ends up in docroot)
        \-resources
            \-js (Here they go your js files)
            \-comp (Here your composite components)
            \-css (Here your css)

Later on, you'll have to take care of avoiding the specific ids in your composites, as JSF modifies them while rendering. Your best is to pass the current component reference to your JS functions:

<h:inputText styleClass="myInputStyle" onclick="showInputText(this)" />

Just refer to included CSS styles and JS functions.

Last but not least, be careful when including the jar as a web resource, if the file paths remain in conflict with the ones in your web app, they won't be included.

See also:

You can include into the facelets wich uses your component an external javascript file by adding the following code:

    <script src="#{request.contextPath}/jspath/yourjs.js"></script>

Within the component when you generate the XHTML output give an Id to your menu entries e.g.

    <h:outputText id="myid" value="#{bean.value}"/>

and in yourjs.js

    $(document).ready(function() {

    $("#myid").click(function(){
    // dostuff
    });
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!