blob: 67ab24c5f877e9302cd70983010a68818ec7fc7f [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= Grails Web 框架简介
:jbake-type: tutorial
:jbake-tags: tutorials
:jbake-status: published
:icons: font
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:description: Grails Web 框架简介 - Apache NetBeans
:keywords: Apache NetBeans, Tutorials, Grails Web 框架简介
本文档将向您介绍有关 NetBeans IDE 中的 link:http://www.grails.org[+Grails+] 的基础知识。Grails 的网站中这样写道:“Grails 旨在为 Groovy 带来‘按规范编码’的范例。它是一个开源 Web 应用程序框架,利用 Groovy 语言并补充了 Java Web 开发的不足。可将 Grails 用作一个独立的开发环境,其中隐藏了所有的配置详细信息或整合了 Java 业务逻辑。Grails 旨在尽可能地简化开发,让更多的开发人员对它感兴趣,而不仅仅局限于那些 Java 社区的成员。”
image::images/netbeans-stamp-80-74-73.png[title="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0"]
*要学习本教程,您需要具备以下软件和资源。*
|===
|软件或资源 |要求的版本
|link:https://netbeans.org/downloads/index.html[+NetBeans IDE+] |版本 7.2 Java 或更高版本
|link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[+Java 开发工具包 (JDK)+] |版本 7
|link:http://www.grails.org/Installation[+Grails+] |当前版本
|===
*注:*
* 您需要将 JDK 7 用作 Java 平台。Grails 当前不在 JDK 8 上运行。
* 您需要激活 Groovy 插件才能在 IDE 中开发 Grails 应用程序。
* 打开 IDE 的“选项”窗口,然后在“其他”类别的 "Groovy" 标签中设置 Grails 的位置。
如果要使用 Groovy,还应将 Groovy 的位置设定在此处。
* 请阅读 link:http://grails.org/Quick%20Start[+Grails 快速入门+],其中包括我们将在本文中重现的场景。
== 创建应用程序
在此部分,您将使用此 "Grails Application"Grails 应用程序)项目模板在 IDE 中运行 "grails create-app" 命令,以创建一个新的 Grails 应用程序。
1. 在本地系统上安装 Grails 并确认其正确配置。
通过从命令行运行 Grails(即,在命令提示处键入 "grails"),可以确认其正常运行。
. 选择 "File"(文件)> "New Project"(新建项目)(Ctrl-Shift-N)并从 "Groovy" 类别中选择 "Grails Application"Grails 应用程序)。单击 "Next"(下一步)。
. "Project Name"(项目名称)中键入 "BookDemo";在 "Project Location"(项目位置)中,选择要在其中创建应用程序的文件夹位置。单击 "Finish"(完成)。
IDE 将运行 "grails create-app" 命令,并在 "Output"(输出)窗口显示输出。"Projects"(项目)窗口应显示以下内容:
image::images/grails-65-4.png[]
展开文件夹,查看 IDE 通过 Grails 脚本创建的源结构。也看一下生成的文件,可以注意到许多生成的文件已经填充了默认值。
== 创建域类
在此部分,您将使用 IDE 运行 "grails create-domain-class" 脚本。
1. 右键单击该域类节点并选择 "New"(新建)> "Grails Domain Class"Grails 域类)。
2. 将该域类命名为 "Book" 并单击 "Finish"(完成)。"Book.groovy" 域将创建在该域类节点中。
3. 打开 Book 类并填入两个字符串 "title" "author"。现在,您应看到如下所示的内容:
image::images/grails-65-8.png[]
. 在位于 "Configuration"(配置)节点中的 Bootstrap.groovy 类中创建了一些初始值。添加到该类的代码以黑体显示如下:
[source,java]
----
class BootStrap {
def init = { servletContext ->
*new Book(author:"Stephen King",title:"The Shining").save()
new Book(author:"James Patterson",title:"Along Came a Spider").save()*
}
def destroy = {
}
}
----
*注:*如果您在源代码编辑器的左旁注中看到警告,则为 ``bookdemo.Book`` 添加导入。
== 创建控制器
在此部分,您将使用 "grails create-controller" 脚本在域类中创建控制器:
1. 右键单击 "Controllers"(控制器)节点并选择 "New"(新建)> "Grails Controller"Grails 控制器)。
2. "Class Name"(类名)中键入 "Book",注意到生成的类将被命名为 "BookController"
image::images/grails-65-11.png[]
单击 "Finish"(完成)。已生成控制器。
. 注释掉在花括号内所生成的一行代码,然后添加 "def scaffold = Book"。现在,您应看到如下所示的内容:
image::images/grails-65-13.png[]
== 运行应用程序
现在,您已经完成了这个简单的 Grails 应用程序。在此部分,您将对其进行部署。
1. 右键单击该应用程序并选择 "Run"(运行)。该应用程序将被部署到 Jetty,如 "Services"(服务)窗口所示:
image::images/grails-65-19.png[]
. URL 已被输出到 "Output"(输出)窗口。如果浏览器未自动打开,请将该 URL 粘贴到浏览器中,然后会看到该应用程序。单击 "BookController" 链接,您将看到以下内容:
image::images/grails-65-14.png[]
. 单击 "New Book"(新建 Book),程序将创建一个新条目:
image::images/grails-65-15.png[]
. 单击 "Create"(创建)之后,请注意可对该条目进行编辑或删除:
image::images/grails-65-17.png[]
. 同时,该条目将被反映到条目列表中:
image::images/grails-65-18.png[]
link:/about/contact_form.html?to=3&subject=Feedback: NetBeans IDE 6.5 Grails Quick Start Guide[+发送有关此教程的反馈意见+]
== 另请参见
NetBeans IDE 中的 Grails 简介”就到此结束。现在,您了解了如何使用 IDE Grails 框架中创建基本应用程序。
想在 link:https://netbeans.org/[+netbeans.org+] 上了解更多关于 Grails 和 Groovy 技术的信息,请参见link:../java/groovy-quickstart.html[+ Groovy 简介+]。