Created
March 26, 2014 13:35
-
-
Save cescoffier/9783261 to your computer and use it in GitHub Desktop.
This file contains 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
package org.wisdom.tutorial.models; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.validation.constraints.NotNull; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Entity | |
public class Task { | |
@Id | |
public Long id; | |
@NotNull | |
public String label; | |
} |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<title>Welcome to Wisdom</title> | |
<meta charset="UTF-8"/> | |
<meta name="viewport" content="width=device-width, initial-scale=1"/> | |
<link rel="stylesheet" href="/libs/css/bootstrap.css" media="screen"/> | |
<link rel="stylesheet" href="/assets/main.css"/> | |
</head> | |
<body> | |
<div class="container"> | |
<h1><span th:text="${#lists.size(tasks)}">X</span> task(s)</h1> | |
<table class="table table-striped"> | |
<tr th:each="task : ${tasks}"> | |
<td><h4><span th:text="${task.label}">Label</span></h4></td> | |
<td> | |
<form action="tasks.html" | |
th:attr="action=${#routes.route('delete', 'id', task.id)}" | |
method="post"> | |
<input type="submit" value="Delete"/></form> | |
</td> | |
</tr> | |
</table> | |
<h2>Add a new task</h2> | |
<form action="tasks.html" method="POST" th:action="${#routes.route('add')}"> | |
<fieldset> | |
<input type="text" name="task"/> | |
<input type="submit" value="Add"/> | |
</fieldset> | |
</form> | |
</div> | |
<script src="/libs/jquery.min.js"></script> | |
<script src="/libs/js/bootstrap.min.js"></script> | |
</body> | |
</html> |
This file contains 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
package org.wisdom.tutorial.controllers; | |
import org.wisdom.api.DefaultController; | |
import org.wisdom.api.annotations.*; | |
import org.wisdom.api.http.HttpMethod; | |
import org.wisdom.api.http.Result; | |
import org.wisdom.api.model.Crud; | |
import org.wisdom.api.templates.Template; | |
import org.wisdom.tutorial.models.Task; | |
@Controller | |
public class TasksController extends DefaultController { | |
@View("tasks") | |
Template template; | |
@Model(Task.class) | |
Crud<Task, Long> tasks; | |
@Route(method= HttpMethod.GET, uri="/tasks") | |
public Result index() { | |
System.out.println(tasks.findAll()); | |
return ok(render(template, "tasks", tasks.findAll())); | |
} | |
@Route(method = HttpMethod.POST, uri="/tasks") | |
public Result add(@Attribute("task") String task) { | |
Task t = new Task(); | |
t.label = task; | |
tasks.save(t); | |
flash().success("Task created"); | |
return index(); | |
} | |
@Route(method = HttpMethod.POST, uri="/tasks/{id}") | |
public Result delete(@Parameter("id") long id) { | |
Task task = tasks.findOne(id); | |
if (task == null) { | |
flash().error("Task not found"); | |
} else { | |
tasks.delete(task); | |
} | |
return index(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment