【问题标题】:Grails not finding my resourcesGrails 找不到我的资源
【发布时间】:2014-11-12 10:40:16
【问题描述】:

我的 Grails 项目结构:

my-app/
    grails-app/
        <mostly typical grails-app structure>
        views/
            web/
                index.gsp
            app/
            admin/
            layouts/
                main.gsp
    <rest of project structure is like any other Grails app>

所以你可以看到,通常索引页位于grails-app/views/index.gsp,我在grails-app/views/web/index.gsp

我的application.properties

app.grails.version=2.4.2
app.name=my-app
app.context=/
app.version=0.1

UrlMappings.groovy的一段:

"/"(view:"/web/index")

我的main.gsp 布局:

<!DOCTYPE html>
<html lang="en">
<head>
    <title><g:layoutTitle default="MyApp"/></title>
    <g:resource dir="css" file="myapp.css" />
    <g:layoutHead/> 
</head>
<body>
    <g:layoutBody/>
    <g:resource dir="js" file="myapp.js" />
</body>
</html>

我的index.gsp

<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main"/>
</head>
<body>
    <h1>Hello!</h1>
</body>
</html>

当应用程序启动并在浏览器中查看它时,很明显没有找到myapp.css,因为页面样式完全错误。当我查看页面源代码时,我看到:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>MyApp</title>

    /css/myapp.css
    <meta name="layout" content="main"/>
</head>
<body>
    <h1>Hello!</h1>
    /js/myapp.js
</body>
</html>

所以听起来 Grails 将您的 app.context 用作所有资源的前缀。而且因为我没有正确连接某些东西,Grails 只是将我的 &lt;g:resource&gt; 标记转换为纯文本并在 HTML 中打印出来。

我想我的问题是:我需要做什么才能让 Grails 找到我的 CSS/JS 资源?

【问题讨论】:

    标签: css grails gsp url-mapping


    【解决方案1】:

    我不确定标签中的标签是否能正常工作,所以这样写:

    <link rel="stylesheet" href="${resource(dir:'css', file:'myapp.css')}" charset="utf-8"/>
    

    【讨论】:

      【解决方案2】:

      根据Documentation,资源标签生成一个链接(URI)字符串。可用于 href、JavaScript、Ajax 调用等。

      使用脚本中的资源标签或链接标签来让你的css/js加载到你的页面中,

      <link rel="stylesheet" href="<g:resource dir="css" file="myapp.css" />" charset="utf-8"/>
      

      您也可以使用resources plugin,我会推荐它,因为它解决了目的并且具有良好的documentation

      【讨论】:

      • Asset Pipeline 插件已取代资源,成为 Grails 的 CSS 和 Javascript 文件的主要解决方案。我个人觉得它比资源插件好得多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多