2013-02-03 34 views
1

我是新來的primefaces,並嘗試用jsf實現一個簡單的web-app。在這個應用程序中,我有一個login.xhtml用戶可以輸入名稱和密碼。如果登錄成功,我的LoginBean類將他重定向到另一個名爲index.xhtml的文件夾basicSites。 雖然在login.xhtml上一切正常,但在index.xhtml發生了錯誤。重定向到這個網站後,我只看到一個白色的網站,瀏覽器控制檯告訴我有一個引用錯誤,因爲jQuery沒有定義。我通過其他許多有類似問題的文章進行了研究,但我的問題仍未解決。重定向後不包含jquery(primefaces)

下面是一些代碼片段,以便更好地理解。

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

回答

4

嘗試增加xmlns:p="http://primefaces.org/ui"到您的網頁

如果上面不會幫助:

您還可以添加

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

要將<h:head>,應該包括與primefacs捆綁在一起的jQuery

+0

感謝您的回答:DI在我的代碼中已經有了xmlns:p ...,但好像我忘了它發佈...對不起^^我已經看到手冊包含h:outputScript也在(( – jennymo

+0

@BlaBlub嘗試將您的代碼放入js文件幷包含它...(將您的js代碼移到單獨的.js文件中) – Daniel

+0

啊,好主意:D這樣做後,我不會在瀏覽器控制檯中得到錯誤。但我仍然沒有看到任何東西...... -.-有可能整個primefaces資源被重定向丟失了嗎?因爲當我用簡單的htmltextline替換index.xhtml中的完整 ...塊時,例如Hello World我可以在重定向後看到它... xmlns:p =「http:// primefaces。 org/ui「包含在我的文件中... – jennymo