1.添加验证码

Application Controller添加captcha()

  1. public static void captcha() {
  2. Images.Captcha captcha = Images.captcha();
  3. renderBinary(captcha);
  4. }

  

添加Route

  1. GET /captcha Application.captcha

  

访问 http://localhost:9000/captcha

验证码图片已经实现了,现在需要做的是验证输入信息与验证码一致

修改captcha()方法

  1. public static void captcha(String id) {
  2. Images.Captcha captcha = Images.captcha();
  3. String code = captcha.getText("#E4EAFD");
  4. Cache.set(id, code, "10mn");
  5. renderBinary(captcha);
  6. }

  

修改show()方法

  1. public static void show(Long id) {
  2. Post post = Post.findById(id);
  3. String randomID = Codec.UUID();
  4. render(post, randomID);
  5. }

  

修改show.html页面

在Comment下方添加验证码图片,和验证控件

  1. <p>
  2. <label for="content">Your message: </label>
  3. <textarea name="content" id="content">${params.content}</textarea>
  4. </p>
  5. <p>
  6. <label for="code">Please type the code below: </label>
  7. <img src="@{Application.catcha(randomId)}">
  8. <br />
  9. <input type="text" name="code" id="code" size="18" value="" />
  10. <input type="hidden" name="randomId" value="${randomId}" />
  11. </p>
  12. <p>
  13. <input type="submit" value="Submit your comment" />
  14. </p>

  

2.验证

修改postComment 方法

  1. public static void postComment(
  2. Long postId,
  3. @Required(message="Author is required") String author,
  4. @Required(message="A message is required") String content,
  5. @Required(message="Please type the code") String code,
  6. String randomId) {
  7. Post post = Post.findById(postId);
  8. validation.equals(code, Cache.get(randomId)).message("Invalid code. Please type it again");
  9.  
  10. if(validation.hasErrors()) {
  11. render("Application/show.html", post);
  12. }
  13.  
  14. post.addComment(author, content);
  15. flash.success("Thanks for posting %s", author);
  16. Cache.delete(randomId);
  17. show(postId);
  18. }

  

修改show.html页面

  1. #{ifErrors}
  2. <p class="error">
  3. ${errors[0]}
  4. </p>
  5. #{/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. 把DATATABLE,DS中的内容用HTML的方式显示

    前几天,在搞一个数据显示的时候,因为是不固定的列的,所以需要动态创建列,这里面就运用一下,直接把数据库的Table显示在Html上,有两种方法,但是都有相应的缺点,第一个,如果内容太多,长度不好控制, ...

  2. EntityFramework 分页问题探讨之 OrderBy

    应用场景 最近被应用程序中页面加载慢的问题所折磨,看似容易的问题,其实并不容易(已经持续两天时间了),经过"侦查",发现了两个"嫌疑犯": EntityFram ...

  3. Java:IDEA下使用JUNIT

    JUnit单元测试--IntelliJ IDEA (本文转载自华行天下) 单元测试的基本使用 一.环境配置 使用idea IDE 进行单元测试,首先需要安装JUnit 插件. 1.安装JUnit插件步 ...

  4. MyEclipse打不开jsp文件 报错“Failed to create the part's controls"

    本来写好了一套网站html代码,需要移植到myeclipse的jsp页面中,当复制代码完毕后,也是可以运行的并不报错,但一直有弹框提示有空指针,当关闭页面后再次打开就歇菜了,提示 我预感到这可能是my ...

  5. Kooboo CMS - Html.FrontHtml.Position 详解

    DataContract 数据契约 http://www.cnblogs.com/Gavinzhao/archive/2010/06/01/1748736.html https://msdn.micr ...

  6. TCP滑动窗口机制

    我们可以大概看一下上图的模型: 首先是AB之间三次握手建立TCP连接.在报文的交互过程中,A将自己的缓冲区大小(窗口大小)3发送给B,B同理,这样双方就知道了对端的窗口大小. A开始发送数据,A连续发 ...

  7. c#动态加载卸载DLL的方法

    这篇文章介绍了c#动态加载卸载DLL的方法,有需要的朋友可以参考一下 c#中通过反射可以方便的动态加载dll程序集,但是如果你需要对dll进行更新,却发现.net类库没有提供卸载dll程序集的方法.在 ...

  8. 数据结构:单链表结构字符串(python版)

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  9. powershell脚本,命令行参数传值,并绑定变量的例子

    这是小技巧文章,所以文章不长.但原创唯一,非常重要.我搜了下,还真没有人发 powershell怎样 [命令行 参数 绑定],所以我决定写成博客. 搜索关键字如下: powershell 命令行 参数 ...

  10. php实现设计模式之 单例模式

    <?php /*单例模式:作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.(创建型模式) * * */ class singleton{ pr ...