简体   繁体   中英

How do I create a proper grails domain class in IntelliJ and have it link to view and its respective controller?

This is my first time asking a question, so feedback would be helpful. I'll try to follow the guidelines as best as possible.

I am following a tutorial from Grails Cookbook to develop a form / database app. The tutorial had me create a Domain class first, then the View, then the Controller. When I reached the Controller section, I was defining the save() method where a new Person object would be created. IntelliJ marks = new Person(params) as an error saying that it can't resolve Person as a symbol (so very descriptive).

I noticed from a separate tutorial I followed that all the Domain classes I made had a database icon next to the file and was linked appropriately to the controller(s) whereas mine just has a Groovy file icon with the class and variable names greyed out indicating a link to the controller does not exist.

I have already tried rebuilding, refreshing Gradle, checking the project structure so it is similar to the project structure of my "working" tutorial, and running the project to see if I was just missing out on exception handling and it was actually working as expected.

I am using SDKMAN as an environment manager for Java 8 open, Groovy 2.4.7, and Grails 3.2.4.

IntelliJ's project structure reports: 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() { }
}

Upon running the app, I expected none of the bootstrapped data for new Person (which also displays in error red) would be viewable in the database console, but turns out had been saved. This is also possible since the Person class under the Domain registered as a database with its respective fields and is viewable in the database console as well.

What is now confusing is how the PersonController behaves. The landing page has an "Available Controllers" section where a link is presented to redirect to http://localhost:8080/Person and does not display the html form and presents its own errors separate from IntelliJ's:

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

I realize Invalid action name: index might be a dead give away that the view is not mapped correctly under the UrlMappings file, but I am lost as it is and, from my limited experience in Grails, don't know if this is related to the primary issue I am having.

params.age is String datatype but Person.age is Int datatype.

Use dataBinder

to make strings onto objects and the necessary types (data binding).

// 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() { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM