我在显示.gsp文件时遇到一些问题,但我不确定为什么。我有以下代码:
class UrlMappings{
static mappings = {
"/"(controller: 'index', action: 'index')
}
}
class IndexController{
def index(){
render(view: "index")
}
}
然后在grails-app / views / index中,我有index.gsp:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>
当我打http://localhost:8080/时,我得到一个500状态代码错误。但是,如果我将IndexController更改为
render "Hello World"
它将显示“Hello World”,因此该应用似乎正在启动。
有人知道发生了什么吗?部分堆栈跟踪:
17:09:40.677 [http-nio-8080-exec-1] ERROR o.a.c.c.C.[.[.[.[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name '/index/index' in servlet with name 'grailsDispatcherServlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name '/index/index' in servlet with name 'grailsDispatcherServlet'
最佳答案
您收到的错误是因为Grails
无法找到您的 View 位置。
Well avoid the names which have some predefined context in the framework(Just an suggestion not an problem in your case).
As you have used the
index
for controller change it to something else
因此,在您遇到
URL
http://localhost:8080/的情况下,您的URLMapping
会将其重定向到 Controller 的index
操作,并将呈现相应的 View 。像下面
class UrlMappings{
static mappings = {
"/"(controller: 'provision', action: 'index')
}
}
class ProvisionController{
def index(){
// You don't really need to render it grails will render
// it automatically as our view has same name as action
render(view: "index")
}
}
然后在
grails-app/views/provision/
中创建index.gsp
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>
您在错误的位置添加了 View
grails-app/views/index.gsp
将其移至grails-app/views/provision/index.gsp
Renamed your
IndexController
toProvisionController
in above example.
关于grails - Grails WebApp不显示GSP页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39775240/