(Quick Reference)

2.4 A Hello World Example - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

2.4 A Hello World Example

To implement the typical "hello world!" example run the create-controller command:

สำหรับการสร้าง "hello world!" นั้นทำได้โดยการรันคำสั่ง create-controller:


grails create-controller hello

This will create a new controller (Refer to the section on Controllers for more information) in the
คำสั่งนี้จะสร้างคอนโทรเลอร์ใหม่ (สามารถอ้างอิงในส่วนนี้ได้ใน Controllers)ใน grails-app/controllers
directory called
ไดเรกทอรี่ที่เรียกว่า helloworld/HelloController.groovy.

If no package is specified with create-controller script, Grails automatically uses the application name as the package name. This default is configurable with the grails.project.groupId attribute in Config.groovy.

ถ้าหากว่าไม่ได้กำหนดชื่อของแพกเกจในคำสั่ง create-controller Grails จะใช้ชื่อของแอพพลิเคชั่นเป็นชื่อของแพกเกจ ค่านี้ถูกกำหนดใน grails.project.groupId ในไฟล์ Config.groovy

Controllers are capable of dealing with web requests and to fulfil the "hello world!" use case our implementation needs to look like the following:

คอนโทรเลอร์มีความสามารถในการจัดการกับ web requests และ ตัวอย่างของ "hello world!" โดยจะมีตัวอย่างโค๊ดดังต่อไปนี้

package helloworld

class HelloController {

def world() { render "Hello World!" } }

Job done. Now start-up the container with another new command called run-app:

หลังจากนี้ทำการรีสตาท์คอนเทนเนอร์โดยใช้คำสั่งนี้ run-app:


grails run-app

This will start-up a server on port 8080 and you should now be able to access your application with the URL:

คำสั่งนี้จะทำการเริ่มเครื่องแม่ข่ายที่พอทร์ 8080 และคุณจะสามารถเข้าไปยังแอพพลิเคชั่นที่เพิ่งสร้างขึ้นได้ดัง URL: http://localhost:8080/helloworld

The result will look something like the following screenshot:

ผลลัพท์จะเห็นได้ดั่งภาพตัวอย่างนี้

This is the Grails intro page which is rendered by the web-app/index.gsp file. You will note it has a detected the presence of your controller and clicking on the link to our controller we can see the text "Hello World!" printed to the browser window.

หน้านี้เป็นหน้าเริ่มต้นของ Grails ที่แสดงผลจาก web-app/index.gsp คุณจะเห็นได้จากว่าหน้านี้ได้แสดงคอนโทรเลอร์ที่เพิ่งสร้างขึ้น และคุณสามารถคลิกเพื่อเข้าไปดูข้อความ "Hello World!" ที่แสดงผลได้ที่หน้าต่าง