重定向后不包含 jquery(primefaces)

标签 jquery jsf redirect primefaces defined

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

以下是一些代码片段,可以帮助您更好地理解。

登录.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

最佳答案

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

如果上述内容没有帮助:

您还可以添加

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

致您的<h:head> ,应该包括与 primefacs 捆绑在一起的 jQuery

关于重定向后不包含 jquery(primefaces),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14672836/

相关文章:

javascript - 一旦我为变量赋值( var userid = ${userid}; ), $(document).ready(function() 就会停止工作

javascript - jQuery UI 破坏了 Visual Studio 2015 Intellisense

java - 验证失败后重置 selectOneMenu 表单值

java - 了解 Spring 重定向

c# - 程序集绑定(bind)和重定向

apache - 使用 http 将域重定向到使用 https 的不同域

javascript - 淡出并多次替换一个div

带有远程验证 MVC 3 的 jQuery 验证组

jsf - Java EE6> 将 JSF facelets (xhtml) 和 ManagedBeans 打包为 JAR

jsf - 如何使用<ui :repeat> to iterate over a nested list?