首页 文章

资源未找到错误以及资源插件怎么样

提问于
浏览
3

我用Grails已近一年了 . 从现在开始我想在gsp中链接css或js文件 . 我做了以下事情:

  • 我在web-app文件夹下创建了一个新文件(例如资源文件),我把所有文件夹放在那里(例如,当导入bootstrap时,我在资源下有一个父文件夹引导程序,在bootstrap下有css,img和js文件夹与他们的文件) .

  • 然后,要导入css文件,我执行了以下操作(here is documentation for this):

<link rel =“stylesheet”href =“$ {resource(dir:'resources / bootstrap / css',file:'bootstrap.min.css')}”type =“text / css”> <script src =“ $ {resource(dir:'resources / bootstrap / js',file:'bootstrap.min.js')}“> </ script>

这很好用,但当我尝试在Grails 2.2.4中创建一个新项目时,我找到了一个资源未找到错误(404到浏览器和以下控制台) .

ERROR resource.ResourceMeta  - Resource not found: /resources/bootstrap/css/bootstrap.min.css
ERROR resource.ResourceMeta  - Resource not found: /resources/bootstrap/js/bootstrap.min.js
ERROR resource.ResourceMeta  - Resource not found: /resources/bootstrap/css/bootstrap.min.css
ERROR resource.ResourceMeta  - Resource not found: /resources/bootstrap/js/bootstrap.min.js

我意识到控制台中的这些错误曾经来自资源功能,而且一次来自客户端(浏览器)请求的GET .

在查看resources plugin时,我看到他们建议使用js和css文件夹 . 在这两个目录中拆分工具(例如twitter bootstrap)有意义吗?

5 回答

  • 0

    我开始认为最灵活的方法是通过在Tomcat / Grails(如Nginx)之前使用代理(对于所有'resources / *'URI)来提供静态内容,并让Grails处理所有动态内容(for其余的URI) .

    毕竟,使用Nginx提供静态文件比让Tomcat / Grails这样做更有效率 .

    但是,作为一个事后的想法,资源插件迫使你将资源分成三个目录应该是可惜的 - 并且驱动Grails对于使用包含无数文件的Ext.js,WYSIWIG编辑器等简单场景很麻烦......

  • 0

    好吧,我相信我有一个(半)工作解决方案:

    假设我们需要包括Twitter Bootstrap 3和TinyMce

    在webapp目录下,我创建以下目录:

    resources/bootstrap/
    resources/bootstrap/css/
    resources/bootstrap/css/bootstrap.min.css
    resources/bootstrap/fonts/
    resources/bootstrap/fonts/glyphicons-halflings-regular.eot
    resources/bootstrap/fonts/glyphicons-halflings-regular.svg
    resources/bootstrap/fonts/glyphicons-halflings-regular.ttf
    resources/bootstrap/fonts/glyphicons-halflings-regular.woff
    resources/bootstrap/js/
    resources/bootstrap/js/bootstrap.min.js
    resources/jquery/
    resources/jquery/jquery-2.0.3.min.js
    resources/tiny_mce/
    resources/tiny_mce/langs/ /*many files here*/
    resources/tiny_mce/plugins/ /*many files here*/
    resources/tiny_mce/themes/ /*many files here*/
    resources/tiny_mce/utils/ /*many files here*/
    resources/tiny_mce/tiny_mce_popup.js
    resources/tiny_mce/tiny_mce_src.js
    resources/tiny_mce/tiny_mce.js
    

    然后我在ApplicationResources.groovy中声明我的资源

    modules = {
        application {
            resource url:'js/application.js'
        }
    
        jquery {
            resource url:'resources/jquery/jquery-2.0.3.min.js'
        }
    
        bootstrap {
           dependsOn 'jquery'
           resource url:'resources/bootstrap/css/bootstrap.min.css'
           resource url:'resources/bootstrap/js/bootstrap.min.js'
        }
    
        tinymce {
            resource url:'resources/tiny_mce/tiny_mce.js'
        }
    }
    

    在Config.groovy中

    grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*']   /*no changes here*/
    grails.resources.adhoc.excludes = ['/**/langs/**/*.*', '/**/themes/**/*.*']  /*to permit some Ajax calls from tiny_mce.js to relevant resources*/
    grails.resources.debug=true 
    /* 
    this is why I call my solution SEMI working. 
    If set grails.resources.debug to false, TinyMce is NOT working because the above excludes are not active, and I receive 404 errors
    */
    

    然后,在main.gsp中

    <!DOCTYPE html>
        <head>
            <g:javascript library="application"/>
            <g:javascript library="bootstrap"/>
            <g:javascript library="tinymce"/>
    
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
            <title><g:layoutTitle default="Grails"/></title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <link rel="shortcut icon" href="${resource(dir: 'images', file: 'favicon.ico')}" type="image/x-icon">
            <link rel="apple-touch-icon" href="${resource(dir: 'images', file: 'apple-touch-icon.png')}">
            <link rel="apple-touch-icon" sizes="114x114" href="${resource(dir: 'images', file: 'apple-touch-icon-retina.png')}">
            <link rel="stylesheet" href="${resource(dir: 'css', file: 'main.css')}" type="text/css">
            <link rel="stylesheet" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css">
    
            <r:layoutResources />
            <g:layoutHead/>
        </head>
        <body>
            <div id="grailsLogo" role="banner"><a href="http://grails.org"><img src="${resource(dir: 'images', file: 'grails_logo.png')}" alt="Grails"/></a></div>
            <g:layoutBody/>
            <div class="footer" role="contentinfo"></div>
            <div id="spinner" class="spinner" style="display:none;"><g:message code="spinner.alt" default="Loading&hellip;"/></div>
    
            <r:layoutResources />
        </body>
    </html>
    

    并在index.gsp

    <head>
    ...
    <script type="text/javascript">
    $(function() {
        tinymce.init({selector:'textarea'});
    });   
    </script>
    </head>
    <body>
    ...
    <h1>Welcome to Grails</h1>
    check bootstrap - start
        <span class="glyphicon glyphicon-search"></span>
        <button type="button" class="btn btn-default btn-lg">
         <span class="glyphicon glyphicon-star"></span> Star
        </button>
    check bootstrap - stop
    
    <textarea>Your content here.</textarea>
    ...
    </body>
    

    使用上面的内容,我有完全可操作的JQuery,Bootstrap3和TinyMCE但是如果我在Config.groovy中设置了

    grails.resources.debug=true
    

    我收到了与页面加载后TinyMce动态提取的 grails.resources.adhoc.excludes 资源相关的404错误 .

    有线索吗?我非常接近找到解决方案,所以我很乐意得到你的输入这个测试项目可以从这里下载:https://docs.google.com/file/d/0B8epX7R4j7jeaVh5OTFiQlV4V0U/edit?usp=sharing

  • 1

    该问题的另一个答案如下:

    • 清理您的项目

    • 更改'BuildConfig.groovy'并使用更新版本的资源插件

    • 对项目执行刷新依赖项

    现在一切都很好

  • 0

    我有同样的问题,我不知道你有什么设置,但我在mail.gsp页面的顶部有这个:

    <link rel="stylesheet" href="${resource(dir: 'css', file: 'bootstrap.css')}" type="text/css">
    

    (在-tag内)

    如果你需要导入.js文件,这对我有用:

    <script src="${resource(dir: 'js', file: 'bootstrap.js')}"></script>
    

    这是-tag内页面的最底层 .

    我正在使用Grails 2.1.1 .

  • 1

    /css/js 目录是资源插件添加到 Config.groovy 的默认"adhoc resources"模式的一部分 . 如果您想为静态资源使用不同的结构,则必须创建资源定义文件(例如 BootstrapResources.groovy )或将目录结构添加到adhoc模式:

    // What URL patterns should be processed by the resources plugin
    grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*', '/resources/*']
    

    这将使 /web-app/resources 中的所有内容成为adhoc资源,并受资源插件的处理 .

相关问题