这是我第一次提出问题,所以反馈会很有帮助。我会尽量遵循这些指导方针。
我正在关注 Grails Cookbook 的教程开发表单/数据库应用程序。教程让我先创建一个域类,然后是 View ,然后是 Controller 。当我到达 Controller 部分时,我正在定义 save()
新的方法Person
对象将被创建。 IntelliJ 标记 = new Person(params)
作为一个错误,表示它无法解析 Person
作为一个符号(非常具有描述性)。
我从一个单独的教程中注意到,我制作的所有域类在文件旁边都有一个数据库图标,并适本地链接到 Controller ,而我的只有一个 Groovy 文件图标,其中类和变量名称显示为灰色到 Controller 的链接不存在。
我已经尝试过重建、刷新 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 的行为方式。登陆页面有一个“可用 Controller ”部分,其中提供了一个链接以重定向到
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
下 View 未正确映射可能是一个死的放弃。文件,但我迷失了,根据我在 Grails 的有限经验,不知道这是否与我遇到的主要问题有关。
最佳答案
params.age 是 字符串 数据类型,但 Person.age 是 诠释 数据类型。
使用dataBinder
将字符串添加到对象和必要的类型(数据绑定(bind))上。
// 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() { }
}
关于grails - 如何在 IntelliJ 中创建适当的 grails 域类并链接到 View 及其各自的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57013339/