专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > .NET Framework

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

发布时间:2011-06-23 13:56:39 文章来源:www.iduyao.cn 采编人员:星星草
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}

 

 

 

 

 

..

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: