我的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/