我正在遵循Grails Cookbook的教程来开发表单/数据库应用程序。本教程让我首先创建一个Domain类,然后创建View,然后创建Controller。当到达控制器部分时,我正在定义save()
方法,将在其中创建新的Person
对象。 IntelliJ将= new Person(params)
标记为错误,表示它无法将Person
解析为符号(描述性很强)。
我在另一本教程中注意到,我遵循的所有Domain类在文件旁边都有一个数据库图标,并已正确链接到控制器,而我的只是一个带有类和变量名的Groovy文件图标。灰色表示不存在到控制器的链接。
我已经尝试过重建,刷新Gradle,检查项目结构,使其类似于我的“工作”教程的项目结构,并运行该项目以查看我是否只是错过了异常处理,而实际上工作正常。
我将SDKMAN用作Java 8 open,Groovy 2.4.7和Grails 3.2.4的环境管理器。
IntelliJ的项目结构报告:Gradle: antlr:antlr:2.7.7
//Person class under the domain folder (and companyname folder and app folder)
package companyname.app
class Person {
String firstName
String lastName
int age
static constraints = {
}
}
// PersonController class where Person(params) is throwing the error
package compannyname.app
class PersonController {
def form() {
}
def save() {
def person = new Person(params) //This is where the IDE gets angry
person.save()
render "Success!"
}
def index() { }
}
运行该应用程序时,我预计new Person
(也显示为红色错误)的自举数据在数据库控制台中均无法查看,但事实证明已保存。这也是可能的,因为Person
下的Domain
类已注册为具有各自字段的数据库,并且也可以在数据库控制台中查看。
现在令人困惑的是PersonController的行为方式。着陆页上有一个“可用的控制器”部分,其中显示了一个链接以重定向到http://localhost:8080/Person
,并且不显示html表单,并且显示其自身的错误(与IntelliJ分开):
URI
/Person
Class
java.lang.IllegalArgumentException
Message
Invalid action name: index
Trace
Line | Method
->> 186 | invoke in org.grails.core.DefaultGrailsControllerClass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 90 | handle in org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter
| 963 | doDispatch . . . in org.springframework.web.servlet.DispatcherServlet
| 897 | doService in ''
| 970 | processRequest . in org.springframework.web.servl
我意识到Invalid action name: index
可能是一个致命的遗弃,因为视图没有正确地映射到UrlMappings
文件下,但是我迷失了自己,并且由于我在Grails方面的有限经验,所以不要不知道这是否与我遇到的主要问题有关。
答案 0 :(得分:0)
params.age是 String 数据类型,而Person.age是 Int 数据类型。
在对象和必要的类型(数据绑定)上创建字符串。
// PersonController class where Person(params) is throwing the error
package compannyname.app
import grails.web.databinding.DataBinder
class PersonController {
def bindPerson = ['firstName','lastName','age']
def form() {
}
def save() {
Person person = new Person() //This is where the IDE gets angry
bindData(person, params, [include: bindPerson])
person.save()
render "Success!"
}
def index() { }
}