gpt4 book ai didi

Facelets 中未定义 Javascript 方法

转载 作者:行者123 更新时间:2023-11-30 13:00:58 25 4
gpt4 key购买 nike

FireFox 说我的方法 doSend 未定义,但它已定义。
Chrome 对 websockets 只字不提……

这是我的代码:

<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>

我应该如何解决这个问题?

最佳答案

在 Facelets 中,<ui:composition> 之外的任何内容模板客户端的 被忽略。基本上,你需要把内容放在<ui:define>里面的 <ui:composition> .

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

  1. 直接写在<ui:define> .

    <ui:composition template="./aTemplate.xhtml">
    <ui:define name="S1">
    <script type="text/javascript">
    ...
    </script>
    ...
    </ui:define>
    </ui:composition>
  2. 同样的方法,但是使用 <h:outputScript target="head">而不是 <script> .它会自动在 HTML 头部结束(前提是您在主模板中使用 <h:head> 而不是 <head> aTemplate.xhtml):

    <ui:composition template="./aTemplate.xhtml">
    <ui:define name="S1">
    <h:outputScript target="head">
    ...
    </h:outputScript>
    ...
    </ui:define>
    </ui:composition>
  3. 添加一个新的 <ui:insert name="head"><h:head> 的底部你的主模板aTemplate.xhtml并在那里声明:

    <ui:composition template="./aTemplate.xhtml">
    <ui:define name="head">
    <script type="text/javascript">
    ...
    </script>
    </ui:define>

    <ui:define name="S1">
    ...
    </ui:define>
    </ui:composition>

另见:


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

<ui:composition template="/WEB-INF/aTemplate.xhtml">

否则最终用户可以单独打开它并查看原始(未解析的)源代码。

另见:

关于Facelets 中未定义 Javascript 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17425136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com