在Grails3中将静态文件呈现为URI
在Grails2.x中,以下工作:在Grails3中将静态文件呈现为URI,grails,grails-3.1,Grails,Grails 3.1,在Grails2.x中,以下工作: class UrlMappings { static mappings = { /** * Serving the index.html directly */ "/"(uri: "/ng/app/index.html") } } 考虑到webapp/ng/app/目录中有一个index.html文件可用。现在当我们浏览URLhttp://localhost:8080在Gr
class UrlMappings {
static mappings = {
/**
* Serving the index.html directly
*/
"/"(uri: "/ng/app/index.html")
}
}
考虑到webapp/ng/app/
目录中有一个index.html
文件可用。现在当我们浏览URLhttp://localhost:8080
在Grails2中,index.html
自动呈现
在Grails3中,我在src/main/webapp/
中添加了相同的index.html
文件,我可以像http://localhost:8080/static/index.html
因此,我正试图在UrlMappings.groovy中做同样的事情:
class UrlMappings {
static mappings = {
/**
* Serving the index.html directly
*/
"/"(uri: "/static/index.html")
}
}
但这给了我错误{“消息”:“内部服务器错误”,“错误”:500}
:
ERROR org.grails.web.errors.GrailsExceptionResolver - UrlMappingException occurred when processing request: [GET] /
Unable to establish controller name to dispatch for [null]. Dynamic closure invocation returned null. Check your mapping file is correct, when assigning the controller name as a request parameter it cannot be an optional token!. Stacktrace follows:
grails.web.mapping.exceptions.UrlMappingException: Unable to establish controller name to dispatch for [null]. Dynamic closure invocation returned null. Check your mapping file is correct, when assigning the controller name as a request parameter it cannot be an optional token!
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我已经用Grails 3.1.8 Angular profile创建了我的应用程序,后来删除了Grails GSP插件、资产管道等与角度相关的东西。这似乎是Grails 3的一个公开问题,计划在Grails 3.2.0中解决
自Grails 3.x实现URLMapping处理程序以来,您只能在URL映射中指定控制器
、视图
或重定向
。因此,您的uri
将失败,因为它不会映射到任何控制器或视图
尽管在修改了源代码之后,我发现了一个变通方法,它可能会在这里对您的用例有用。实际上,您可以在此处发出重定向
,因为重定向将能够处理uri
因此,您的URLMappings.groovy
应该是-->
这段代码适用于我使用grails3.1.8
我希望这能有所帮助。请看:谢谢@DrewBeres的回复。我已经知道这个解决方案,我有另一种通过文件流提供静态内容的方法,但不幸的是,我希望这个解决方案能像Grails2.5.4一样工作。不幸的是,这似乎对我不起作用。它仍然将我重定向到/static/index.html
,而不是直接提供index.html
。到目前为止,您不能简单地以这种方式提供静态文件。但是如果您将重定向
与uri
一起使用,它将不会抛出您得到的异常。如果你想使用URLMapping做同样的事情,这只是一个解决办法。哦!我认为您的解决方案不是用于重定向。这已经对我起作用了,
“/”(重定向:'/ng/app/index.html')确切地说,如果我们的应用程序是SPA,这至少可以避免我们使用控制器来服务索引页面
class UrlMappings {
static mappings = {
/**
* Serving the index.html directly
*/
"/"(redirect: [uri: "/static/index.html"])
}
}