【问题标题】:Import bootstrap components in JSF [duplicate]在 JSF 中导入引导组件 [重复]
【发布时间】:2015-03-24 07:26:48
【问题描述】:

嗯,我正在尝试将引导程序与 JSF 项目一起使用。我正在使用这个引导布局:http://startbootstrap.com/template-overviews/sb-admin-2/

所以,我正在尝试导入 metisMenu 但没有成功,请参阅:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>

    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="" />
    <meta name="author" content="" />

    <title>Projeto A</title>

    <h:outputStylesheet library="js" name="bootstrap-3.3.2-dist/css/bootstrap.min.css" />   
    <h:outputStylesheet library="js" name="metisMenu/metisMenu.min.css" />
    <h:outputStylesheet library="css" name="sb-admin-2.css" />

    <h:outputScript library="js" name="jquery-2.1.3.min.js" />  
    <h:outputScript library="js" name="bootstrap-3.3.2-dist/js/bootstrap.min.js" /> 
    <h:outputScript library="js" name="metisMenu/metisMenu.min.js" />
    <h:outputScript library="js" name="sb-admin-2.js" />

</h:head>

<body>


</body>

</html>

在正文中我有一个和平的侧面菜单

<div class="navbar-default sidebar" role="navigation">
                <div class="sidebar-nav navbar-collapse">
                    <ul class="nav" id="side-menu">
                        <li class="sidebar-search">
                            <div class="input-group custom-search-form">
                                <input type="text" class="form-control" placeholder="Search..." />
                                <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <i class="fa fa-search"></i>
                                </button>
                            </span>
                            </div>
                            <!-- /input-group -->

但我在浏览器控制台上收到以下错误:

Uncaught TypeError: undefined is not a function in $('#side-menu').metisMenu()

查看我的 JS:

$(function() {

    $('#side-menu').metisMenu();

});

//Loads the correct sidebar on window load,
//collapses the sidebar on window resize.
// Sets the min-height of #page-wrapper to window size
$(function() {
    $(window).bind("load resize", function() {
        topOffset = 50;
        width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
        if (width < 768) {
            $('div.navbar-collapse').addClass('collapse');
            topOffset = 100; // 2-row-menu
        } else {
            $('div.navbar-collapse').removeClass('collapse');
        }

        height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1;
        height = height - topOffset;
        if (height < 1) height = 1;
        if (height > topOffset) {
            $("#page-wrapper").css("min-height", (height) + "px");
        }
    });

    var url = window.location;
    var element = $('ul.nav a').filter(function() {
        return this.href == url || url.href.indexOf(this.href) == 0;
    }).addClass('active').parent().parent().addClass('in').parent();
    if (element.is('li')) {
        element.addClass('active');
    }
});

【问题讨论】:

  • 啊,那行...不是很有用,因为我们对整个页面一无所知。你为什么不发sscce.org
  • 不高兴这是更重要的问题和平,这已经是一个sscce。
  • 你好。我也有同样的问题。你解决了这个吗?怎么样?

标签: twitter-bootstrap jsf-2 primefaces


【解决方案1】:

有几件事可能会导致这种情况,回答这个问题本质上就像在黑暗中做空。

检查以下内容:

1) 当您在浏览器中检查页面时,#side-menu 的 jsf id 是什么,它是否有类似 formId:panelId:side-menu 的 id - 如果是这样,您的 init 代码应该引用带有转义 id 的组件,例如 formId\:panelId\:side-menu 否则它不会看到组件。请记住,尽管给出了一个 id,但如果一个组件有一个 jsf 元素(这在 2.2 中是可能的),它将被分配一个 jsf 组件 id。

2) 你在哪里初始化 $('#side-menu').metisMenu(); - 它在 document.ready 中吗?如果不是在执行此代码时,组件将不会呈现,因此无法用于初始化。

我个人会将所有脚本文件移到页面底部,因为它会影响页面加载速度,并且脚本在所有组件都加载到文档中之前是无用的。

如果上述方法均无效,请添加整个页面的代码,或者至少添加我们可以看到可能导致此问题的部分的代码。

【讨论】:

  • 1 - 我使用的是 HTML 而不是 JSF 组件,所以我不需要放置“formId\:panelId”。 2 - 它在 document.ready 我将发布我的所有页面。
  • 我用侧边菜单 UL 添加了更多代码。
  • 我创建了一个与您的项目类似的项目,并且一切正常 - 所以这段代码没问题。唯一的区别是你使用primefaces,但我没有。我还看到您包含 jQuery 库,但是当您使用 primefaces 时,它会注入其捆绑的 jQuery 库,然后与您添加的库发生冲突:linklink。删除您的 jQuery 库并使用添加的一个 primefaces 运行程序。
  • 您可能还需要使用jQuery(..) 而不是$ 表示法,就好像我记得在我的primefaces 时代它不支持$,或者执行link 之类的操作。希望它会工作,保持发布。
  • 删除您添加的 jQuery 库是否解决了问题?
猜你喜欢
  • 2020-07-12
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2018-05-07
  • 2015-11-24
  • 2018-07-11
  • 1970-01-01
相关资源
最近更新 更多