Facelets中未定义Javascript方法

Facelets中未定义Javascript方法,javascript,jsf,facelets,Javascript,Jsf,Facelets,FireFox说我的方法doSend没有定义,但确实有定义。 Chrome没有提到WebSocket 这是我的密码: <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui" xmlns:c="http://jav

FireFox说我的方法doSend没有定义,但确实有定义。
Chrome没有提到WebSocket

这是我的密码:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<script type="text/javascript">

    var image;
    var output;
    function init() {
        console.log('CONNECTED to websockets!');
        output = document.getElementById('output');
        testWebSocket();
    }

    //connects websocket to KwetterWebsocketBean
    function testWebSocket() {
        websocket = new WebSocket(wsUri);
        websocket.onopen = function(evt) {
            onOpen(evt);
        };
        websocket.onclose = function(evt) {
            onClose(evt);
        };
        websocket.onmessage = function(evt) {
            onMessage(evt);
        };
        websocket.onerror = function(evt) {
            onError(evt);
        };
    }

    //define event handlers
    function onOpen(evt) {
    }            
    function onWindowClose(evt){
        websocket.close();
    }
    function onClose(evt) {
    }
    function onMessage(evt) {
        //convert json to javascript object
        var message = JSON.parse(evt.data);
        writeToScreen('<span style="color: green;">New tweet by ' + message.username + ': ' + message.text + '</span>');
        console.log(message.text + ' : ' + message.username);
        //write message.text to screen
    }

    function onError(event) {
    }

    function doSend(message) {
        console.log(message);
        websocket.send(message);
    }

    //appends text to #output
    function writeToScreen(text) {
        var pre = document.createElement('p');
        pre.style.wordWrap = 'break-word';
        pre.innerHTML = text;
        output.appendChild(pre);
    }

    //invoke init() on load
    window.addEventListener('load', init, false);

    //enter key clicks #sendButton
    function keyPressed(event){
        if(event.keyCode == 13){
            document.getElementById('sendButton').click();
            document.getElementById('textforws').value='';
        }
    }
</script>
</h:head>
<h:body>
    <ui:composition template="./aTemplate.xhtml">
        <ui:define name="S1">
            <h1>What's happening?</h1>
            <h:form id="formNewestTweet">
                <p:inputText maxlength="140" value="aMessage"/>
                <p:commandButton value="Post Tweet" id="sendButton"                                     
                                 onclick='doSend("aMessage");'/>
            </h:form>
            <div id="output"/>
        </ui:define>
    </ui:composition>
</h:body>
</html>

var图像;
var输出;
函数init(){
log('连接到WebSocket!');
output=document.getElementById('output');
testWebSocket();
}
//将websocket连接到KwetterWebsocketBean
函数testWebSocket(){
websocket=新的websocket(wsUri);
websocket.onopen=函数(evt){
onOpen(evt);
};
websocket.onclose=函数(evt){
onClose(evt);
};
websocket.onmessage=函数(evt){
onMessage(evt);
};
websocket.onerror=函数(evt){
onError(evt);
};
}
//定义事件处理程序
功能开启(evt){
}            
函数onWindowClose(evt){
websocket.close();
}
函数onClose(evt){
}
消息函数(evt){
//将json转换为javascript对象
var message=JSON.parse(evt.data);
writeToScreen(“+message.username+”的新推文:“+message.text+”);
console.log(message.text+':'+message.username);
//将message.text写入屏幕
}
函数onError(事件){
}
函数doSend(消息){
控制台日志(消息);
发送(消息);
}
//将文本附加到#输出
功能写入屏幕(文本){
var pre=document.createElement('p');
pre.style.wordWrap='break word';
pre.innerHTML=文本;
输出.appendChild(pre);
}
//加载时调用init()
addEventListener('load',init,false);
//输入键单击#发送按钮
功能键按下(事件){
如果(event.keyCode==13){
document.getElementById('sendButton')。单击();
document.getElementById('textforws')。值=“”;
}
}
发生了什么事?

如何修复此问题?

尝试在

  <head></head>

标签

因为head标记中包含的所有脚本都是在body标记之前呈现的


以及主体内部的事件。

在Facelets中,模板客户端的
之外的任何内容都将被忽略。基本上,您需要将内容放入

在您的特定情况下,有几种方法可以修复此错误代码:

  • 把它直接放进

    
    ...
    ...
    
  • 同样的方法,但是使用
    而不是
    。它将自动在HTML头中结束(前提是在主模板
    aTemplate.xhtml
    中有一个
    而不是
    ):

    
    ...
    ...
    
  • 将新的
    添加到主模板
    底部
    aTemplate.xhtml
    ,并在其中声明:

    
    ...
    ...
    
  • 另见:

    与具体问题无关,我建议将主模板文件放在
    /WEB-INF
    文件夹中,并像这样使用它

    <ui:composition template="/WEB-INF/aTemplate.xhtml">
    
    
    
    否则,最终用户可以单独打开它并查看原始(未解析)源代码

    另见:

    在以后的问题中,请不要使用
    [XHTML]
    标记而不是
    [Facelets]
    将Facelets过度概括为XHTML。天真的HTML/JS开发人员将XHTML过度概括为过度炒作的HTML,但他们并不了解真正的XHTML。请参见在和之间添加了脚本。它仍然不起作用。试着阅读我的答案。我说没有地方在脚本周围放置
    。或者,如果你指的是其他人的答案,请向他们发表评论,而不是向我发表评论。@BalusC该评论是针对另一个答案的,我现在正在尝试你的答案。以后,直接在其他人的答案中发表评论,而不是回复我的评论。