Can't execute Javascript in jsf page

后端 未结 2 1670
南方客
南方客 2021-02-09 12:30

I\'m new to jsf. I have been trying to do a simple Javascript function with commandbutton. I tried many times but wasn\'t even able to do an alert message. This is part of my co

相关标签:
2条回答
  • 2021-02-09 13:02

    Any thing defined outside ui:composition is ignored.So place your content inside this tag and it will work: Something like this:

    <ui:composition template="/WEB-INF/tags/layout.xhtml"> 
    <ui:include src="/tags/common.xhtml"></ui:include>  
    <ui:define name="content">
    <h:outputScript name="validation.js" library="javascript"></h:outputScript>
    
    0 讨论(0)
  • 2021-02-09 13:07

    Apart from this lowercase/uppercase typo (which wouldn't cause the function not being called at all, by the way), your concrete problem is caused because this page is been designed as a template client using <ui:composition>. Any content outside the <ui:composition> tag is ignored during runtime by Facelets. This content is only useful for visual web designers, but once it's compiled and executed during runtime, it's ignored altogether. Instead the composition's content will be inserted in the master template, the commonLayout in your case.

    You need to put the <script> element inside an <ui:define> instead, this way it will be taken into the final Facelets composition.

    <ui:define name="...">
        <script>
            ...
        </script>
        ...
    </ui:define>
    

    Or, better, put the JS function in its own JS file in the /resources folder and reference it as follows:

    <ui:define name="...">
        <h:outputScript name="some.js" target="head" />
        ...
    </ui:define>
    

    Thanks to the target="head", it'll automatically be relocated into the HTML <head> during building the view.

    See also:

    • How to include another XHTML in XHTML using JSF 2.0 Facelets?
    0 讨论(0)
提交回复
热议问题