css - 找不到我的资源

标签 css grails gsp url-mapping

我的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只是将<g:resource>标记转换为纯文本并以HTML格式打印出来。

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

最佳答案

我不确定将标签插入标签是否可以正常工作,因此请这样编写:

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

关于css - 找不到我的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26884945/

相关文章:

datetime - Grails-如果少于10分钟,则比较Groovy日期时间

javascript - 粘性标题没有完全固定

html - :hover, 焦点等所有状态的 css 通配符?

html - 如何在 div 保持不变的情况下将文本包装在 div 中?

maven - 如何更改下载的 Maven 依赖项的位置?

grails - 通过metaClass在Grails电子邮件插件中添加/修改密件抄送

css - Font-Awesome 重叠图标

email - 使用布局渲染 gsp/view 不起作用

javascript - Grails,通过单击表格行中的列来执行javascript

grails - 根据是否包含gsp值更改grails行颜色