Skip to content

Instantly share code, notes, and snippets.

@cescoffier
Created March 26, 2014 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cescoffier/9783227 to your computer and use it in GitHub Desktop.
Save cescoffier/9783227 to your computer and use it in GitHub Desktop.
<!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>
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;
}
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