Play Framework 完整實現一個APP(七),frameworkapp

來源:互聯網
上載者:User

Play Framework 完整實現一個APP(七),frameworkapp

 

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}

 

 

 

 

 

..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.