【问题标题】:jquery(primefaces) is not included after redirect重定向后不包括 jquery(primefaces)
【发布时间】:2013-02-03 13:12:41
【问题描述】:

我是 primefaces 的新手,我尝试用 jsf 实现一个简单的网络应用程序。在这个应用程序中,我有一个 login.xhtml,用户可以在其中输入姓名和密码。如果登录成功,我的 LoginBean 类会将他重定向到文件夹 basicSites 中名为 index.xhtml 的另一个页面。 在 login.xhtml 上一切正常,在 index.xhtml 上发生错误。重定向到该站点后,我只看到一个白色站点,并且浏览器控制台告诉我存在引用错误,因为未定义 jQuery。我研究了许多其他有类似问题的文章,但我的问题仍然没有解决。

这里有一些代码 sn-ps 以便更好地理解。

login.xhtml

<h:head>
    <title>Zugangsverwaltung | Login</title>
    <link type="text/css" rel="stylesheet" href="#{request.contextPath}/default.css" />
    <link type="text/css" rel="stylesheet" href="#{request.contextPath}/syntax.css" />
    <script type="text/javascript" href="#{request.contextPath}/scripts.js"/>
</h:head>

<h:body>
    <script type="text/javascript"> 
        jQuery(document).ready(function () {
            jQuery('#logoutLink').fadeOut();
        });
        function handleLoginRequest(xhr, status, args) { 
            if(args.validationFailed || !args.loggedIn) {  
                jQuery('#dialog').effect("shake", { times:3 }, 100);  
            } else {  
                dlg.hide();  
                jQuery('#loginLink').fadeOut(0); 
                jQuery('#logoutLink').fadeIn();
            }  

        } </script>

LoginBean.java

public void login(ActionEvent actionEvent) {
    RequestContext context = RequestContext.getCurrentInstance();
    FacesMessage msg = null;
    boolean loggedIn = false;

    FacesContext ctx = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) ctx.getExternalContext().getRequest();

    try {
        request.login(username, password);
        loggedIn = true;
        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Willkommen", username);
        FacesContext.getCurrentInstance().addMessage("global", msg);
    } catch (ServletException e) {
        loggedIn = false;
        msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login fehlgeschlagen!", "Invalid credentials");
        FacesContext.getCurrentInstance().addMessage("local", msg);
    }

    try {
        if (loggedIn) {
            ExternalContext ectx = ctx.getExternalContext();
            ectx.redirect(request.getContextPath() + "/basicSites/index.xhtml");
        }
    } catch (Exception e) {
        System.out.println("LoginBean - login: " + e.getMessage());
    }

并且至少是发生错误的 index.xhtml:

<h:head>
    <title>Zugriffsverwaltung | Backend</title>
    <link type="text/css" rel="stylesheet" href="#{request.contextPath}/default.css" />
    <link type="text/css" rel="stylesheet" href="#{request.contextPath}/syntax.css" />
    <script type="text/javascript" href="#{request.contextPath}/scripts.js" />
</h:head>

<h:body>
    <script type="text/javascript"> 
        jQuery(document).ready(function () { <!-- Here: JQuery is not definded --> 
            jQuery('#logoutLink').fadeOut();
        });</script>

    <p:layout fullPage="true" >
        <p:layoutUnit id="center" position="center"> <!-- ... -->

我希望任何人都知道现在该做什么;)感谢每一个帮助我解决问题的提示。

提前致谢:D

【问题讨论】:

    标签: jquery jsf redirect primefaces defined


    【解决方案1】:

    尝试将xmlns:p="http://primefaces.org/ui" 添加到您的页面

    如果以上没有帮助:

    你也可以加

    <h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />
    

    致您的 &lt;h:head&gt; ,其中应包括与 primefacs 捆绑的 jQuery

    【讨论】:

    • 感谢您的回答 :D 我的代码中已经有 xmlns:p...,但似乎我忘记发布...抱歉 ^^ 我看过手册包括与 h:outputScript 也在许多其他文章中,当然尝试过,但它仍然对我不起作用:((
    • @BlaBlub 尝试将您的代码放入 js 文件并包含它...(将您的 js 代码移动到单独的 .js 文件中)
    • 啊,好主意 :D 这样做之后,我在浏览器控制台中没有收到错误消息。但我仍然没有看到任何东西...... -.- 重定向会丢失完整的 primefaces 资源吗?因为当我用一个简单的 htmltextline 替换 index.xhtml 中的完整 ... 块时,例如 Hello World 我可以在重定向后看到它。 ..xmlns:p="primefaces.org/ui" 包含在我的文件中...
    • 你看到了什么?如果您查看页面的源代码,您是否看到原始的&lt;p:layout&gt; 标签?如果是这样,您还没有在类路径中包含 primefaces jar...添加它 - 将其放入您的 lib 文件夹...
    • 我已经在 lib 文件夹中包含了 jar 并将其添加到我的类路径中;-) 幸运的是,我发现错误位于我的 faces-config 中,现在它可以正常工作了!感谢您的帮助:)
    猜你喜欢
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2017-06-09
    • 1970-01-01
    相关资源
    最近更新 更多