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(七)的更多相关文章

  1. Play Framework 完整实现一个APP(十一)

    添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...

  2. Play Framework 完整实现一个APP(五)

    程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...

  3. Play Framework 完整实现一个APP(二)

    1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...

  4. Play Framework 完整实现一个APP(十四)

    添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...

  5. Play Framework 完整实现一个APP(十三)

    添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...

  6. Play Framework 完整实现一个APP(十二)

    1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...

  7. Play Framework 完整实现一个APP(十)

    1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...

  8. Play Framework 完整实现一个APP(九)

    添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...

  9. Play Framework 完整实现一个APP(八)

    创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...

随机推荐

  1. 窥探Swift之基本数据类型

    在上一篇博客“窥探Swift编程之在Playground上尽情的玩耍”中介绍了如何使用Playground来学习Swift语言.本篇博客就使用Playground来窥探Swift语言.千里之行始于足下 ...

  2. 【url重写】

    一.原理void Application_BeginRequest(object sender, EventArgs e)    {  //url重写        HttpApplication a ...

  3. 基于Hadoop生态技术构建阿里搜索离线系统

    一.计算平台架构 平台架构 集群规模 集群特点 二.支撑的搜索业务 搜索业务 处理流程 三.YARN计算平台 iStream计算模型 Schedule改进 AppHistoryServer改进 HSt ...

  4. 敏捷开发Scrum

  5. 代码的坏味道(2)——过大的类(Large Class)

    坏味道--过大的类(Large Class) 特征 一个类含有过多字段.函数.代码行. 问题原因 类通常一开始很小,但是随着程序的增长而逐渐膨胀. 类似于过长函数,程序员通常觉得在一个现存类中添加新特 ...

  6. HTTP请求方法详解

    HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定 ...

  7. Visula Studio 2013/2015自定义快捷键

    很多同学新装了VS2013/2015后, 发现快捷键变掉了, 比如之前编译快捷键是F6, 现在变成Ctrl + Shift + B, 其实要改回去很简单, 菜单Tools->Options, 打 ...

  8. 月经贴——.net前景何妨!

    已经从业7年了,除了.net什么也不会.思索.net前景也挺长时间了.很少人有主动改变的动力,边思索边在.net中沉迷.现在反应学.net的人越来越少了,而做企业的人还找不到做.net的.总是感觉现在 ...

  9. EntityFramework查询--联合查询(Join,GroupJoin)

    首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...

  10. IIS8 添加配置 WCF服务

    今天在Windows8.1 操作系统部署了半天的WCF 一直老是在报错.在这里做个记录 防止下次忘记 在网上查了半天.终于知道原来IIS8不支持WCF服务SVC的请求.所以必须要给IIS8添加WCF服 ...