javascript - 单击 h :commandLink causes Uncaught ReferenceError: mojarra is not defined

标签 javascript jsf undefined mojarra commandlink

我知道 this发帖,我仔细检查了那里的所有可能性。

我在 Glassfish 3 上使用 JSF 2.0 和 Mojarra 实现。

我正在尝试使用两个简单的 <h:commandLink>标签更改应用程序语言。 这是 .xhtml页:

<!DOCTYPE html>
<html lang="en"  xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>
        <h:outputText value = "#{appMessage['page.welcome']}" />
    </title>

    <f:metadata>
        <f:event type = "preRenderView" listener = "#{sessionController.changeLanguage}" />
    </f:metadata>
</h:head>

<h:body>
    <h1><h:outputText value = "#{appMessage['text.slide.welcome']}" /></h1>

    <h:form id = "fm-language">
        <h:commandLink action = "#{sessionController.changeLanguage('en')}" value = "#{appMessage['link.english']}" />
        <h:commandLink action = "#{sessionController.changeLanguage('de')}" value = "#{appMessage['link.german']}" />
    </h:form>

</h:body>

这是 HTML 代码:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>The Maze Project</title>
    </head>
    <body>
        <h1>Welcome</h1>
        <form id="fm-language" name="fm-language" method="post" action="/maze/welcome.xhtml" enctype="application/x-www-form-urlencoded">
            <input type="hidden" name="fm-language" value="fm-language" />
            <script type="text/javascript" src="/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces">
            </script>
            <a href="#" onclick="mojarra.jsfcljs(document.getElementById('fm-language'),{'fm-language:j_idt13':'fm-language:j_idt13'},'');return false">English</a>
            <a href="#" onclick="mojarra.jsfcljs(document.getElementById('fm-language'),{'fm-language:j_idt15':'fm-language:j_idt15'},'');return false">Deutsch</a>            
            <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="8038443616162706480:-1387069664590476821" autocomplete="off" />
        </form>
</body>

当按下 commandLink 时,什么也没有发生。没有请求发送到服务器,抛出以下 Java Script 错误:

mojarra is not defined

bean 方法被正确调用并在应用程序的其余部分正常工作。

最佳答案

源代码和生成的 HTML 输出看起来不错,你有一个 <h:head>在 JSF 源代码中(否则 JSF 无法自动包含任何 CSS/JS 文件),以及 javax.faces:jsf.js HTML 输出中存在脚本。

你说,你有一个 JS 错误 mojarra未定义。那只能意味着下面的自动生成的脚本

<script type="text/javascript" src="/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces">
</script>

没有产生有效的响应。这反过来只能意味着你有一个 Filter映射到 /**.xhtml这是限制 jsf.js以某种方式请求资源。也许是一些本土的身份验证过滤器没有完全正确地完成它的工作。尝试打开

http://localhost:8080/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces

在您的浏览器中查看实际检索到的内容(或使用网络开发人员工具检查响应)。如果它确实不是正确的响应并且问题确实在 Filter ,那么您可能需要重写它,以便在请求 URI 以 ResourceHandler.RESOURCE_IDENTIFIER 开头时它应该继续链。 .

例如

HttpServletRequest req = (HttpServletRequest) request;

if (req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response); // Let it continue.
    return;
}

关于javascript - 单击 h :commandLink causes Uncaught ReferenceError: mojarra is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248047/

相关文章:

javascript - qx.log.appender 语法

java - 无法在静态 Java 方法中获取数据源

java - 没有 faces-config 的 JSF 2.0 国际化

vb.net - 'objType' 未定义...实际上,它是,那么为什么会发生这种情况?

url - Socket.io 在 URL 中调用 http ://undefined/. ...

jquery - 有时会出现"jQuery is not defined"错误

javascript - 从函数 Javascript 设置变量 PHP 的值

javascript - 在 fetch React-Native 中返回代码

javascript - Angular 2 : Uncaught SyntaxError: Unexpected token <

jsf - primefaces 数据表/排序/错误的项目