单击h:commandLink会导致未捕获的ReferenceError:未定义mojarra

Ion*_*nut 8 javascript jsf undefined commandlink mojarra

我知道这篇文章,我仔细检查了那里的所有可能性.

我在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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

按下commandLink时根本不会发生任何事情.没有发送到服务器的请求,并抛出以下Java Script错误:

mojarra没有定义

正确调用bean方法,并在其余的应用程序中正常工作.

Bal*_*usC 5

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

你说,你得到了一个mojarra没有定义的JS错误.这只能意味着以下自动生成的脚本

<script type="text/javascript" src="/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces">
</script>
Run Code Online (Sandbox Code Playgroud)

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

HTTP://本地主机:8080 /迷宫/ javax.faces.resource/jsf.js.xhtml LN = javax.faces

在浏览器中查看它实际检索到的内容(或使用Web开发人员工具检查响应).如果它确实不是正确的响应并且确实存在问题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;
}
Run Code Online (Sandbox Code Playgroud)