我的控制器中无法访问域类

发布于 2024-10-30 03:12:06 字数 400 浏览 1 评论 0原文

我是 Grails 和 groovy 的新手..
我创建了一个域类

\grails-app\domain\Abc

现在我在

\grails-app\controllers\myapp\myController

其中创建了一个控制器,当我创建一个对象时它显示错误。

def Abc obj = new Abc

我得到的错误是

unable to resolve class Abc 

我尝试导入,但也没有显示在那里。我使用 grails 1.3.7 和 IntelliJ IDEA 10.0.2
谢谢

I'm new to Grails and groovy..
I created a domain class

\grails-app\domain\Abc

Now i created a controller in

\grails-app\controllers\myapp\myController

In that, when i created an object it shows error.

def Abc obj = new Abc

The error i got is

unable to resolve class Abc 

I tried to import, but didn't shown there also. Me working in grails 1.3.7 and IntelliJ IDEA 10.0.2
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-11-06 03:12:06

确保每个类的位置与类和包声明一致。

我创建了一个域类

\grails-app\domain\Abc

这个类应该看起来像这样,

class Abc {
    // implementation omitted
}

确保您没有包声明,因为根据位置,这个类应该位于默认包中(这实际上是一个不好的做法)。理想情况下,您应该将此类放入包中,然后将源文件移动到与包名称相对应的 \grails-app\domain 子目录中。

现在我创建了一个控制器

\grails-app\controllers\myapp\myController

这个类应该看起来像这样

package myapp
  
class myController {
    // implementation omitted
}

请注意,这个类应该用小写的“m”命名,因为这就是文件的命名方式。标准 Java/Groovy 命名约定规定类应以大写字母开头。

其中,当我创建一个对象时,它显示错误。

def Abc obj = new Abc

此代码有几个问题:

  • 将类型定义为 def
    Abc 但不是两者都
  • 缺少一些括号

试试这个:

Abc obj = new Abc()

Make sure that the location of each class is consistent with the class and package declaration.

I created a domain class

\grails-app\domain\Abc

This class should look like this

class Abc {
    // implementation omitted
}

make sure you have no package declaration, because based on the location, this class should be in the default package (which is actually a bad practice). Ideally you should put this class into a package, then move the source file into a subdirectory of \grails-app\domain that corresponds to the package name.

Now i created a controller in

\grails-app\controllers\myapp\myController

This class should look like this

package myapp
  
class myController {
    // implementation omitted
}

Notice that this class should be named with a lower-case 'm' because that's how the file is named. The standard Java/Groovy naming conventions dictate that classes should begin with a capital letter.

In that, when i created an object it shows error.

def Abc obj = new Abc

There are a couple of problems with this code:

  • define the type as either def or
    Abc but not both
  • you're missing some parentheses

Try this instead:

Abc obj = new Abc()
暮年慕年 2024-11-06 03:12:06

我认为您创建了您的域类 &手动控制器类。首先,您的控制器应该位于 controllers 文件夹中,而不是 myapp 中。其次,您应该为您的域类和域类定义一个包。控制器类,例如:

领域类:

package myapp

class Book {
...
}

控制器类

package myapp

class BookController {
....
}

I think you created your domain class & controller class manually. First, your controller should be in the folder controllers, not myapp. Second, you should define a package for both your domain class & controller class, for example:

Domain class:

package myapp

class Book {
...
}

Controller class

package myapp

class BookController {
....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文