• 技术文章 >Java >java教程

    PlayFramework完整实现一个APP(七)

    黄舟黄舟2016-12-23 16:42:55原创488
    1.添加验证码

    application Controller添加captcha()

    public static void captcha() {
    Images.Captcha captcha = Images.captcha();
    renderBinary(captcha);
    }

      

    添加Route

    GET /captcha Application.captcha

      

    访问 http://localhost:9000/captcha

    212.png

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

    修改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}

    以上就是PlayFramework完整实现一个APP(七)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:PlayFramework APP
    上一篇:PlayFramework完整实现一个APP(六) 下一篇:PlayFramework完整实现一个APP(八)
    大前端线上培训班

    相关文章推荐

    • 理解java8中java.util.function.*pojo反射新方法(附代码)• 浅析安卓app和微信授权登录及分享完整对接(代码分享)• 教你一招搞定时序数据库在Spring Boot中的使用• 一招教你使用java快速创建Map(代码分享)• PlayFramework 完整实现一个APP(十一)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网