Last active
December 13, 2015 05:33
-
-
Save satomikko94/eba9a0bba3b67e13265f to your computer and use it in GitHub Desktop.
PlayFrameworkでキャッシュの値の有無によってViewを出し分けするサンプル ref: http://qiita.com/satomikko94/items/c46be49f8833a03339e1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static Form<Task> taskFrom = Form.form(Task.class); | |
| public Result index() { | |
| return ok(setCache.render(taskFrom)); | |
| } | |
| public Result setTask() { | |
| // get values from form | |
| Form<Task> filledForm = taskFrom.bindFromRequest(); | |
| Task task = filledForm.get(); | |
| // set Task model to cache | |
| Cache.set("task", task, 60 * 1); | |
| return redirect(routes.Application.index()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @(taskForm: Form[Task]) | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <title>Play framework - Cache sample</title> | |
| </head> | |
| <body> | |
| <!-- send data from form --> | |
| @form(routes.Application.setTask()) { | |
| @inputText(taskForm("id")) | |
| @inputText(taskForm("taskName")) | |
| <input type="submit"> | |
| } | |
| <!-- show cache data --> | |
| @showCache() | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @(task : Task = Cache.get("task").asInstanceOf[Task]) | |
| <!-- if cache is not null, show cache data --> | |
| @if(task == null) { | |
| <h1>Cache has no data!</h1> | |
| } else { | |
| <h1>Task data from Cache</h1> | |
| id : @task.id<br> | |
| taskName : @task.taskName<br> | |
| <br> | |
| Cache will expire after 1 minutes. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Task { | |
| public String id; | |
| public String taskName; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment