tailieunhanh - The definitive guide to grails second edition - phần 3

có chứa cấu hình cho từng môi trường (xem hình 2-11). Bạn có thể tìm thấy thuận tiện này, bởi vì nó có nghĩa là hầu hết công việc được thực hiện cho bạn, nhưng bạn có thể thích sử dụng một cơ sở dữ liệu như MySQL chứ không phải là HSQLDB các cơ sở dữ liệu được cung cấp. | CHAPTER 4 UNDERSTANDING CONTROLLERS 95 The next step is to consider how to enable users to register log in and log out. Before you can do that you need to define the notion of a user within the gTunes application. Let s do that in the next section. Adding the User Domain Class To model users you ll need to create a User domain class that contains personal information such as first name and last name as well as the login and password for each user. To do so you can use the create-domain-class command grails create-domain-class This will create a new domain class at the location grails-app domain com g2one gtunes . With that done you need to populate the User domain class with a few properties as shown in Listing 4-47. Listing 4-47. The User Domain Class package class User String login String password String firstName String lastName static hasMany purchasedSongs Song As you can see the code in Listing 4-47 captures only the basics about users but you could easily expand this information to include an address contact number and so on. One property to note is the purchasedSongs association which will hold references to all the Songs a User buys once you have implemented music purchasing. However before we get too far ahead of ourselves let s add a few constraints to ensure domain instances stay in a valid state see Listing 4-48 . Listing 4-48. Applying Constraints to the User Class class User . static constraints login blank false size matches S unique true password blank false size matches S firstName blank false lastName blank false With these constraints in place you can ensure that a user cannot enter blank values or values that don t fall within the necessary size constraints. Also note the usage of the unique 96 CHAPTER 4 UNDERSTANDING CONTROLLERS constraint which ensures that the login property is unique to each User. We ll revisit this in more detail later for now let s focus on login and .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.