Play Framework 完整实现一个APP(七)
1.添加验证码
Application Controller添加captcha()
public static void captcha() {
Images.Captcha captcha = Images.captcha();
renderBinary(captcha);
}
添加Route
GET /captcha Application.captcha
访问 http://localhost:9000/captcha

验证码图片已经实现了,现在需要做的是验证输入信息与验证码一致
修改captcha()方法
public static void captcha(String id) {
Images.Captcha captcha = Images.captcha();
String code = captcha.getText("#E4EAFD");
Cache.set(id, code, "10mn");
renderBinary(captcha);
}
修改show()方法
public static void show(Long id) {
Post post = Post.findById(id);
String randomID = Codec.UUID();
render(post, randomID);
}
修改show.html页面
在Comment下方添加验证码图片,和验证控件
<p>
<label for="content">Your message: </label>
<textarea name="content" id="content">${params.content}</textarea>
</p>
<p>
<label for="code">Please type the code below: </label>
<img src="@{Application.catcha(randomId)}">
<br />
<input type="text" name="code" id="code" size="18" value="" />
<input type="hidden" name="randomId" value="${randomId}" />
</p>
<p>
<input type="submit" value="Submit your comment" />
</p>
2.验证
修改postComment 方法
public static void postComment(
Long postId,
@Required(message="Author is required") String author,
@Required(message="A message is required") String content,
@Required(message="Please type the code") String code,
String randomId) {
Post post = Post.findById(postId);
validation.equals(code, Cache.get(randomId)).message("Invalid code. Please type it again"); if(validation.hasErrors()) {
render("Application/show.html", post);
} post.addComment(author, content);
flash.success("Thanks for posting %s", author);
Cache.delete(randomId);
show(postId);
}
修改show.html页面
#{ifErrors}
<p class="error">
${errors[0]}
</p>
#{/ifErrors}
..
Play Framework 完整实现一个APP(七)的更多相关文章
- Play Framework 完整实现一个APP(十一)
添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...
- Play Framework 完整实现一个APP(五)
程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...
- Play Framework 完整实现一个APP(二)
1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...
- Play Framework 完整实现一个APP(十四)
添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- Play Framework 完整实现一个APP(十二)
1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...
- Play Framework 完整实现一个APP(十)
1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...
- Play Framework 完整实现一个APP(九)
添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...
- Play Framework 完整实现一个APP(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
随机推荐
- Android随笔之——Android单元测试
在实际开发中,开发android软件的过程需要不断地进行测试.所以掌握Android的单元测试是极其重要的.您应该把单元测试作为Android应用开发周期的一部分,精心编写的测试可以在开发早起帮你发现 ...
- 安装ClouderaManager以及使用ClouderaManager安装分布式集群的若干细节
目录 前言 整体介绍 分步安装介绍 总结 一.前言 周末干了近四十个小时中间只休息了五个小时终于成功安装了ClouderaManager以及分布式集群,其中各种辛酸无以言表,唯有泪两行. ...
- grape动态PHP结构(一)——目录结构与配置文件
一.结构介绍 结构的名字grape,中文名叫葡萄,因为最近一个同事经常带葡萄到公司给我们吃,受到启发想到了这个名字. 1)本结构需要在PHP5.5中运行,如果要在5.4中运行,有些地方就要做些修改 2 ...
- IOS 整体架构 和 MVC布局
IOS的生态系统 IOS生态系统不仅仅是指产品,更重要的是指 iPhone/iPad/iPod/Mac +iCloud+App整个系统,包括Siri (部分设备不支持).FaceTime.Safari ...
- 1Z0-053 争议题目解析304
1Z0-053 争议题目解析304 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 304.What privileges must be granted to allow an a ...
- Java中写文件操作
OutputStream 和 Writer OutputStream类(直接操作byte数组) 该类是字节输出流的抽象类,定义了输出流的各种操作方法.如下图是OutputStream的层次结构: By ...
- git版本回退, github版本回退
上周提交了更改,过了周末回来说要撤销上个story.于是,需要找到上周提交的版本,rollback回来. git版本管理命令,自从习惯使用管理工具之后就很少接触了,当突然寻找其他指令的时候就成浆糊了, ...
- .NET正则表达式基础入门(三)
括号 正则表达式中的括号能将多个字符或者表达式当做一组,即将他们看成一个整体.这样量词就可以修饰这一组表达式.阅读本章前,建议先下载我于CSDN上传的示例代码,下载无需分数,下载链接. 1.分组 假设 ...
- EC笔记,第二部分:8.别让异常逃离析构函数
1.为何析构函数不应该抛出异常? 有两种情况: 1).假设析构函数中有众多语句,而第一条语句抛出异常(或者其他语句),那么抛出异常以后的语句就得不到执行.而通常我们在析构函数中写的是清理资 ...
- php xml 文件读取 XMLReader
php xml 文件读取 <?php /** $xmlString = '<xml> <persons count="10"> <person ...