(七)play之yabe项目【CRUD】
增加CRUD功能
使用CRUD能干嘛?---->
在页面对模型进行增删改查操作,这样有什么实际意义呢?
不使用CRUD模块的功能,只要模型继承了Model类,就有save(),delete()等方法可以调用了
这个CRUD对开发有什么帮助?
用于快速生成一个基本的管理区域,如初始化一个登陆用户,很方便。
到目前为止,我对CRUD功能的理解只能这样:
提供一个可视化的对象管理界面,就好比数据库的客户端,play在后台执行SQL为我们处理数据
而我们可以在页面进行增删改查的操作,不用再写增删改查的代码了!
为了使用CRUD功能,需要导入play的CRUD依赖
修改yabe\conf\dependencies.yml
在require后面添加依赖的模块
- # Application dependencies
- require:
- - play -> crud
注意:dependencies文件中不能使用TAB键,只能使用空格键!!!!!!
运行dependencies命令,安装模块

为CRUD模块添加路由
这条路由将把所有CRUD内建路由导入到/admin为前缀的URL路径中
- #import CRUD module
- * /admin module:crud
刷新eclipse工程,可看到新增了一个modules文件夹,其下放了一个crud的文件
该文件存放了一个路径,指向了本地play解压目录中的\modules\crud文件夹
E:\technology-hqh\soft\play-1.2.5\modules\crud
要想将模块导入,还需要重新执行eclipsify命令
E:\technology-hqh\proj\play-framework\yabe>play eclipsify
刷新工程,这时模块才真正被导入到IDE中,项目中才能找到CRUD这个类!
声明模型所对应的控制器
按照默认约定,控制器的名称为对应模型的复数形式,如User--->Users
当然,也可以自定义名称,只需要使用注解标明这个控制器对应的模型即可
但是,一般还是用默认的算了!呵呵,没那么多精力去研究,先跟着例子跑一遍再说!
User实体类对应的控制器
- package controllers;
- public class Users extends CRUD {
- }
Post实体类对应的控制器
- package controllers;
- public class Posts extends CRUD {
- }
Comment实体类对应的控制器
- package controllers;
- public class Comments extends CRUD {
- }
打开http://localhost:9000/admin/,即可看到系统中所有声明控制器的实体对象了

点击User实体,可以查看当前的对象
这些User对象看起来有点不友好,只有一个简单的序号(id?)来区分,可以对其进行定制
在User实体中复写toString()
- @Override
- public String toString() {
- return "User [" + fullname + "]";
- }
刷新页面

为模型添加验证
既然可以在页面操作对象了,对应的数据校验功能也不可少
使用play,对字段进行校验,只需要使用注解即可,连javascript都不用写
得益于play.data.validation.*这个包下的东东!
对User类的email字段和password进行验证
- package models;
- import java.util.ArrayList;
- import java.util.List;
- import javax.persistence.Entity;
- import javax.persistence.OneToMany;
- import play.data.validation.Email;
- import play.data.validation.Required;
- import play.db.jpa.Model;
- @Entity
- public class User extends Model {
- @Required
- public String email;
- @Required(message="input your pwd now!")
- public String password;
- public String fullname;
- public boolean isAdmin;
- //@OneToMany 声明User与Post之间是1对多的关系
- //mappedBy="author" 表示将通过对方(User)的author字段来进行关联关系的维护
- @OneToMany(mappedBy="author")
- public List<Post> posts;
- public User(String email,String password, String fullname) {
- this.email = email;
- this.password = password;
- this.fullname = fullname;
- this.posts = new ArrayList<Post>(0);
- }
- /**
- * 联合email和password两个条件查询User
- * @param email
- * @param password
- * @return
- */
- public static User connect(String email, String password) {
- return find("byEmailAndPassword", email, password).first();
- }
- /**
- * 添加Post的动作放到User中,这样可以把Post设置到User的List<Post>集合中
- * 这样实现了双方都持有对方的引用了
- * @param title
- * @param content
- * @return
- */
- public User addPost(String title, String content) {
- Post post = new Post(title,content,this).save();
- this.posts.add(post);
- this.save();
- return this;
- }
- @Override
- public String toString() {
- return "User [" + fullname + "]";
- }
- }
什么都不输,保存一个User对象
会提示错误信息到页面的,play真实个大好人啊,什么活都给干了!
为Post实体加入验证
- package models;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Entity;
- import javax.persistence.Lob;
- import javax.persistence.ManyToOne;
- import javax.persistence.OneToMany;
- import play.data.validation.MaxSize;
- import play.data.validation.Required;
- import play.db.jpa.Model;
- @Entity
- public class Post extends Model {
- @Required
- public String title;
- @Required
- public Date postedAt;
- //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容
- @Lob
- @Required
- @MaxSize(100000000)
- public String content;
- //@ManyToOne:声明Post与User之间是多对一的关系
- //一个用户可以发布多个博客,一个博客只能被一个用户所发布
- @Required
- @ManyToOne
- public User author;
- //1篇博客对应多个评论
- //删除某篇博客,则级联删除其评论
- @OneToMany(mappedBy="post", cascade=CascadeType.ALL)
- public List<Comment> comments;
- public Post() {
- System.out.println("create Post");
- this.postedAt = new Date();
- }
- public Post(String title, String content, User author) {
- this.comments = new ArrayList<Comment>(0);
- this.title = title;
- this.content = content;
- this.author = author;
- this.postedAt = new Date();
- }
- /**
- * 在Post中实现评论的添加保存操作
- * 同时更新Post所持有的Comment的集合!
- */
- public Post addComment(String author, String content) {
- //保存对方
- Comment newComment = new Comment(author, content, this).save();
- //把对方加入到自己管理的集合中
- this.comments.add(newComment);
- //同步到数据库
- this.save();
- return this;
- }
- /**
- * 前一篇博文
- */
- public Post previous() {
- return Post.find("postedAt < ? order by postedAt desc", postedAt).first();
- }
- /**
- * 后一篇博文
- */
- public Post next() {
- return Post.find("postedAt > ? order by postedAt asc", postedAt).first();
- }
- }
注意:使用CRUD在页面操作,对象的创建过程:
1.调用默认构造函数生成一个对象
2.查询数据库,把数据赋值到这个对象
3.页面呈现此对象的相关属性
如果在页面新创建一个对象,则不会查询数据库
1.调用默认构造函数生成一个对象
2.为对象赋值(如果默认构造函数中有这样做)
3.页面呈现各个字段为空的对象,如果有为字段设置默认值,可在默认构造函数中操作
4.页面输入数据,保存对象
这里想说的是:对于createTime这样的字段值,一般是后台new Date()创建的,在页面使用CRUD貌似还不能对其进行控制,因为页面会呈现这个字段出现让用户输入(是否可以通过js控制其disabled状态呢?)暂时有这样一个疑问!有人知否?或者play提供了禁止编辑的注解,是什么???
通过message转换窗体显示的Label
在yabe\conf\messages中加入需要转换的字符串
play会自动将页面显示的字符进行替换
- # You can specialize this file for each language.
- # For example, for French create a messages.fr file
- #
- title=Title
- content=Content
- postedAt=Posted at
- author=Author
- post=Related post
- tags=Tags set
- name=Common name
- email=Email
- password=Password
- fullname=Full name
- isAdmin=User is admin
原始的页面

转换后的页面

(七)play之yabe项目【CRUD】的更多相关文章
- (四)play之yabe项目【页面】
(四)play之yabe项目[页面] 博客分类: 框架@play framework 主页面 显示当前发表博客的完整内容,以及历史博客列表 Bootstrap Job 一个play job任务就是 ...
- (八)play之yabe项目【身份验证】
(八)play之yabe项目[身份验证] 博客分类: 框架@play framework 添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...
- (九)play之yabe项目【发表博文】
(九)play之yabe项目[发表博文] 博客分类: 框架@play framework 发表一篇博文 填充管理页面 从主页链接到管理页面时,只简单显示了登陆用户的名称 现在对显示的内容加以丰富 ...
- (六)play之yabe项目【验证码】
(六)play之yabe项目[验证码] 博客分类: 框架@play framework 添加验证码功能 在Application.java中添加一个action:captcha() /** * 添 ...
- (三)play之yabe项目【数据模型】
(三)play之yabe项目[数据模型] 博客分类: 框架@play framework 创建项目 play new yabe What is the application name? [yab ...
- 一张图搞定OAuth2.0 在Office应用中打开WPF窗体并且让子窗体显示在Office应用上 彻底关闭Excle进程的几个方法 (七)Net Core项目使用Controller之二
一张图搞定OAuth2.0 目录 1.引言 2.OAuth2.0是什么 3.OAuth2.0怎么写 回到顶部 1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常 ...
- 七天接手react项目-起步
七天接手react项目-起步 背景 假如七天后必须接手一个 react 项目(spug - 一个开源运维平台),而笔者只会 vue,之前没有接触过 react,此刻能做的就是立刻展开一个"7 ...
- 七天接手react项目 系列 —— react 脚手架创建项目
其他章节请看: 七天接手react项目 系列 react 脚手架创建项目 前面我们一直通过 script 的方式学习 react 基础知识,而真实项目通常是基于脚手架进行开发. 本篇首先通过 reac ...
- 七天接手react项目 系列
七天接手react项目 背景 假如七天后必须接手一个 react 项目(spug - 一个开源运维平台),而笔者只会 vue,之前没有接触过 react,此刻能做的就是立刻展开一个"7天 r ...
随机推荐
- Second Day learning English
Today I have set my Microsoft word program, use it send documents to the blog site.
- 通过 Storyboard 快速搭建一系列连贯性的视图控制器
此例子只是一个简单的 Demo,这里没有过多介绍如何去实现,网上有很多关于 Storyboard 技术的介绍,请自行搜索. 效果如下: iPhone 5s iPhone 6 iPhone 6 ...
- POJ 1650 Integer Approximation
Integer Approximation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5081 Accepted: ...
- 制作便携版 FireFox 火狐浏览器
Firefox是一款可高度自定义的开源浏览器: 你可以访问 火狐DIY 定制自己的Firefox安装包, 此外,你还可以自己动手定制一款可以放在U盘随身携带的便携版Firefox火狐浏览器. 制作便携 ...
- hbase 使用
hbase shell命令的使用 再使用hbase 命令之前先检查一下hbase是否运行正常 hadoop@Master:/usr/hbase/bin$ jps HMaster NameNode Se ...
- 《Programming with Objective-C》第八章 Working with Blocks
Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDicti ...
- @media 手机与IPAD与PC
@media screen and (min-width: 769px) { bindCard{height:28.5em} } @media screen and (min-device-width ...
- NopCommerce插件学习
在园子里看到这篇文章:http://www.cnblogs.com/haoxinyue/archive/2013/06/06/3105541.html写的非常好,我也是在此文章的基础上来一步步的学习N ...
- ECMASCRIPT 6中字符串的新特性
本文将覆盖在ECMAScript 6 (ES6)中,字符串的新特性. Unicode 码位(code point)转义 Unicode字符码位的长度是21位[2].而JavaScript的字符串,是1 ...
- 数据可视化(4)--jqplot
本来打算继续研究Google Charts,但上头下了指示让看jqplot,无奈,只好先将Google Charts放一放,不过真心觉得Google Charts不错,现在先开始jqplot. jqP ...